在android中自动更新电池电量

时间:2013-01-20 10:58:19

标签: android

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

如何自动更新电池电量。

   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);
        }
}

3 个答案:

答案 0 :(得分:2)

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

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);
    }

答案 1 :(得分:1)

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

context.unregisterReceiver(this);

删除它。

答案 2 :(得分:0)

试试这段代码..

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);

  }
 }

   };
}