我如何每秒更新一次电池充电状态

时间:2019-01-24 11:26:04

标签: java xml timer threadpool

我正在开发一个移动应用程序,该应用程序在启动时保持屏幕开通,并且你们所有人都知道这种应用程序会耗尽电池。因此,我只想在插入设备后才将屏幕保持打开状态。我在stackoverflow上找到了这段代码(感谢大家)。

  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
  Context context = this;
  Intent batteryStatus = context.registerReceiver(null, ifilter);
  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
  boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
  int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
  if (isCharging) {
    if (usbCharge) {
      tv1.setText("USB plugged in");
    } else {
      if (acCharge) {
        tv1.setText("AC plugged in");
      }
    }
  } else {
    tv1.setText("Connect your charger");
  }
}

我将此代码放在oncreate中。但是问题是它只显示一次状态(启动应用程序时)。所以我安排了一个计时器任务来重复这样的检查

t = new TimerTask() {
    @Override 
    public void run() {
        runOnUiThread(new Runnable() {
              @Override 
              public void run() {
                  IntentFilter ifilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
                  Context context = this;
                  Intent batteryStatus = context.registerReceiver(null, ifilter);
                  int status = batteryStatus.getIntExtra(BatteryManager.EXTRA_STATUS, -1);
                  boolean isCharging = status == BatteryManager.BATTERY_STATUS_CHARGING || status == BatteryManager.BATTERY_STATUS_FULL;
                  int chargePlug = batteryStatus.getIntExtra(BatteryManager.EXTRA_PLUGGED, -1);
                  boolean usbCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_USB;
                  boolean acCharge = chargePlug == BatteryManager.BATTERY_PLUGGED_AC;
                  if (isCharging) {
                    if (usbCharge) {
                      tv1.setText("USB plugged in");
                    } else {
                      if (acCharge) {
                        tv1.setText("AC plugged in");
                      }
                    }

但这不起作用,并显示错误

  

类型不匹配:无法从新的Runnable(){}转换为上下文

我是新手,所以请看看您是否可以通过一些修改甚至新代码来帮助我。提前致谢。 重要说明:我在Sketchware上工作,它仅允许您将代码放入MainActivity.xml或MainActivity.java中,其他功能(如manifest.xml)不可编辑。回答时请记住这一点。

1 个答案:

答案 0 :(得分:0)

使用广播接收器获取电池状态。 像这样:-

// Initialize a new BroadcastReceiver instance
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver()
{
    @Override
    public void onReceive(Context context, Intent intent)
    {            
        int status=intent.getIntExtra(BatteryManager.EXTRA_STATUS,-1);            
        if(status==BatteryManager.BATTERY_STATUS_CHARGING)
        {
            //Do anything if Charging
        }
        else
        {
            //Do anything if not Charging
        }
    }
}

那样称呼它:-

// Get the application context
Context mContext = getApplicationContext();
// Initialize a new IntentFilter instance
IntentFilter iFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
// Register the broadcast receiver
mContext.registerReceiver(mBroadcastReceiver,iFilter);

希望这对您有帮助!