代码之家  ›  专栏  ›  技术社区  ›  Sarah

电池电量为10时启动/取消计时器

  •  1
  • Sarah  · 技术社区  · 13 年前

    我正在开发一款黑莓应用程序,当电池电量达到10%时,该应用程序会向服务器发送消息。这是通过调用 getBatteryStatus() 方法,其中 获取电池状态() 执行以下操作:

    public static void getBatteryStatus() {
    
          timerBattery = new Timer(); 
          taskBattery = new TimerTask() {
              public void run() {
                  UiApplication.getUiApplication().invokeLater(new Runnable() {
                      public void run() {
                          if (DeviceInfo.getBatteryLevel()==10) {
                             try{
                                String freeText="Battery level at 10%";
                                sendBatteryStatus(freeText);
                             }catch(Exception e){}
                          }
                      }
                  });
              }
          };
    
          timerBattery.scheduleAtFixedRate(taskBattery, 0, 20000);
    }
    

    这个 sendBatteryStatus 将消息发送到服务器并取消计时器。这实际上已经根据请求向服务器发送了一次消息。

    然而,如果用户在应用程序正常运行的情况下(没有再次调用构造函数)开始为手机充电,该怎么办?计时器将如何重新启动?下次我如何才能再次将消息发送到服务器?

    防止在10%的电池电量下发送多条消息,并且实际上只发送一次消息,然后在下次电池电量为10%时再次发送消息的最佳机制是什么?

    如果在发送消息后不取消计时器,则会多次向服务器发送消息。

    1 回复  |  直到 13 年前
        1
  •  3
  •   Nate    13 年前

    事实上,我认为如果你完全去掉计时器,你会过得更好。我不认为计时器能有效地为你提供你想要的一切。

    幸运的是,黑莓拥有 SystemListener 界面这样执行:

    public final class BatteryListener implements SystemListener {
    
        /** the last battery level we were notified of */
        private int _lastLevel = 0;
        /** the battery percentage at which we send an event */
        private int _threshold = 10;
    
        public BatteryListener() {
            Application.getApplication().addSystemListener(this);
        }
    
        public void setThreshold(int value) {
            _threshold = value;
        }
    
        /** call this to stop listening for battery status */
        public void stopListening() {
            Application.getApplication().removeSystemListener(this);
        }
    
        private boolean levelChanged(int status) {
            return (status & DeviceInfo.BSTAT_LEVEL_CHANGED) == DeviceInfo.BSTAT_LEVEL_CHANGED;
        }
    
        public void batteryStatusChange(int status) {
            if (levelChanged(status)) {
                int newLevel = DeviceInfo.getBatteryLevel();
                if (newLevel <= _threshold && _lastLevel > _threshold) {
                    // we have just crossed the threshold, with battery draining
                    sendBatteryStatus("Battery level at " +
                                      new Integer(newLevel) + "%!");
                }
                _lastLevel = newLevel;
            }
        }
    
        public void batteryGood() { /** nothing to do */ }      
        public void batteryLow() { /** nothing to do */ }       
        public void powerOff() { /** nothing to do */ }     
        public void powerUp() { /** nothing to do */ }  
    }
    

    然后,只要你想让你的应用程序 开始 为您监控电池。如果电池电量降至10%,它将发送一条消息。如果用户稍后再次开始充电,然后停止充电,并且再次消耗超过10%,则会发送另一条服务器消息。

    private BatteryListener listener;
    

    listener = new BatteryListener();   // start monitoring
    

    显然,在上面的课程中,您要么必须添加 sendBatteryStatus() 方法传递给类,或者向该类传递一个实现 发送电池状态() 方法

    注: 我还建议你 将您的通知发送到主线程上的服务器。您没有显示的实现 发送电池状态() ,所以也许你已经是了。但如果没有,请使用后台线程通知您的服务器,这样UI就不会被冻结。

    推荐文章