在android中自动更新电池电量

Posted

技术标签:

【中文标题】在android中自动更新电池电量【英文标题】:Automatic update battery level in android 【发布时间】:2013-01-20 10:58:19 【问题描述】:

我正在使用此代码在我的应用中显示电池电量。它工作正常,但不会自动更新电池电量。

如何自动更新电池电量。

   public class MainActivity extends Activity 


        private TextView txtBattery;

        private BroadcastReceiver mBatteryLevelReciver = new BroadcastReceiver() 

            @Override
            public void onReceive(Context context, Intent intent) 

                context.unregisterReceiver(this);
                //int rawLevel = intent.getIntExtra("level", -1);
                int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
                //int scale = intent.getIntExtra("scale", -1);
                int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
                int level = -1;
                if (rawLevel >= 0 && scale > 0) 
                    level = (rawLevel * 100) / scale;
                
                txtBattery.setText("Battery3 Level Remaining  :" + level + "%");
            

        ;

        @Override
        public void onCreate(Bundle savedInstanceState) 

            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);

            txtBattery = (TextView) findViewById(R.id.batterymeter_txt);
            batteryLevel();

        

        private void batteryLevel() 
            IntentFilter batteryLevelFliter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
            registerReceiver(mBatteryLevelReciver, batteryLevelFliter);
        

【问题讨论】:

也许是一个每 x 秒执行一次的计时器(timertask)?还是我误解了这个问题? @Keyser 我刚刚发现了同样的问题here。改变 context.unregisterReceiver(this);但我不知道该怎么做! afaik 一些 android 设备仅在电池以 10 的倍数变化时发送该广播。(即 100%->90%->80% 等)。无论如何,你在哪里创建这个 batteryLevel() ?一个活动 ?服务? 【参考方案1】:

将接收者变量移动为类属性,以便在接收到意图时它将在内存中:

public class Main extends Activity 

    private TextView txtBattery;

    private BroadcastReceiver mBatteryLevelReciver = new BroadcastReceiver() 

        @Override
        public void onReceive(Context context, Intent intent) 

            //int rawLevel = intent.getIntExtra("level", -1);
            int rawLevel = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
            //int scale = intent.getIntExtra("scale", -1);
            int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, -1);
            int level = -1;
            if (rawLevel >= 0 && scale > 0) 
                level = (rawLevel * 100) / scale;
            
            txtBattery.setText("Battery Level Remaining  :" + level + "%");
        

    ;

    @Override
    public void onCreate(Bundle savedInstanceState) 

        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        txtBattery = (TextView) findViewById(R.id.batterymeter_txt);
        batteryLevel();

    

    private void batteryLevel() 
        IntentFilter batteryLevelFliter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        registerReceiver(batteryLevelReciver, batteryLevelFliter);
    

【讨论】:

嗨哈维,我更新了上面的完整代码。请你现在检查一下。还是不行! 您的代码底部可能有 mBatteryLevelReciver 而不是 batteryLevelReciver?但它仍然无法正常工作! 7 个月前here 发布了另一个相同的问题来更改 context.unregisterReceiver(this);但我不知道该怎么做! 对不起,我没看到!!删除此行 context.unregisterReceiver(this); 并重试。【参考方案2】:

为什么要在 onReceive() 方法中取消注册 BroadcastReceiver

context.unregisterReceiver(this);

删除它。

【讨论】:

【参考方案3】:

试试这个代码..

public class AndroidBattery extends Activity 

private TextView batteryLevel, batteryVoltage, batteryTemperature,
batteryTechnology, batteryStatus, batteryHealth;


  /** Called when the activity is first created. */
   @Override
 public void onCreate(Bundle savedInstanceState) 
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   batteryLevel = (TextView)findViewById(R.id.batterylevel);
   batteryVoltage = (TextView)findViewById(R.id.batteryvoltage);
   batteryTemperature = (TextView)findViewById(R.id.batterytemperature);
   batteryTechnology = (TextView)findViewById(R.id.batterytechology);
   batteryStatus = (TextView)findViewById(R.id.batterystatus);
   batteryHealth = (TextView)findViewById(R.id.batteryhealth);

   this.registerReceiver(this.myBatteryReceiver,
     new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
 

 private BroadcastReceiver myBatteryReceiver
 = new BroadcastReceiver()

  @Override
  public void onReceive(Context arg0, Intent arg1) 
   // TODO Auto-generated method stub

  if (arg1.getAction().equals(Intent.ACTION_BATTERY_CHANGED))
 batteryLevel.setText("Level: "
 + String.valueOf(arg1.getIntExtra("level", 0)) + "%");
 batteryVoltage.setText("Voltage: "
   + String.valueOf((float)arg1.getIntExtra("voltage", 0)/1000) + "V");
   batteryTemperature.setText("Temperature: "
  + String.valueOf((float)arg1.getIntExtra("temperature", 0)/10) + "c");
 batteryTechnology.setText("Technology: " + arg1.getStringExtra("technology"));

 int status = arg1.getIntExtra("status", BatteryManager.BATTERY_STATUS_UNKNOWN);
  String strStatus;
 if (status == BatteryManager.BATTERY_STATUS_CHARGING)
  strStatus = "Charging";
   else if (status == BatteryManager.BATTERY_STATUS_DISCHARGING)
  strStatus = "Dis-charging";
   else if (status == BatteryManager.BATTERY_STATUS_NOT_CHARGING)
strStatus = "Not charging";
   else if (status == BatteryManager.BATTERY_STATUS_FULL)
  strStatus = "Full";
  else 
strStatus = "Unknown";
  
  batteryStatus.setText("Status: " + strStatus);

  int health = arg1.getIntExtra("health", BatteryManager.BATTERY_HEALTH_UNKNOWN);
  String strHealth;
  if (health == BatteryManager.BATTERY_HEALTH_GOOD)
  strHealth = "Good";
  else if (health == BatteryManager.BATTERY_HEALTH_OVERHEAT)
  strHealth = "Over Heat";
   else if (health == BatteryManager.BATTERY_HEALTH_DEAD)
  strHealth = "Dead";
   else if (health == BatteryManager.BATTERY_HEALTH_OVER_VOLTAGE)
   strHealth = "Over Voltage";
   else if (health == BatteryManager.BATTERY_HEALTH_UNSPECIFIED_FAILURE)
   strHealth = "Unspecified Failure";
  else
   strHealth = "Unknown";
  
 batteryHealth.setText("Health: " + strHealth);

  
 

   ;

【讨论】:

以上是关于在android中自动更新电池电量的主要内容,如果未能解决你的问题,请参考以下文章

Android_(自动化)自动获取手机电池的剩余电量

android6.0系统Healthd分析及低电量自动关机流程

iOS:监控电池电量使用更多电池?应该自动保存吗?

电池电量低于 40% 时是不是可以关闭蓝牙?

荣耀v7pro使用时间过长怎么解决会不会突然重启

如果被短信、推送通知或电池电量不足等 uialert 中断,如何自动继续录制