2

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

我应该怎么做才能自动更新电池电量。

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

3 回答 3

2

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

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);
    }
于 2013-01-20T11:15:28.750 回答
1

你为什么BroadcastReceiver在你的onReceive()方法中注销你的?

context.unregisterReceiver(this);

删除它。

于 2013-01-20T14:08:49.880 回答
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);

  }
 }

   };
}
于 2013-06-27T08:41:43.437 回答