1

I want to collect how much power use for a app in android. I find that BatteryStatsHelper class in source code, but it need android.permission.BATTERY_STATS. I put it in to AndroidManifest.xml, but it not work. I search this issues and someone say that this permission only give system app. I have other idea to do this. what other good idea for me ?

thanks

1 Answers1

-1

You should register broadcast like this:

public class BatteryActivity extends Activity{

private BatteryReceiver batteryReceiver;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.battary);

    batteryReceiver = new BatteryReceiver();
    IntentFilter batteryFilter = new IntentFilter();
    batteryFilter.addAction(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(batteryReceiver, batteryFilter);
}


private class BatteryReceiver extends BroadcastReceiver{

    @Override
    public void onReceive(Context arg0, Intent arg1) {
        int level = arg1.getIntExtra("level", 0);
        Log.i("Battery", "当前电池的剩余电量为:" +  level + "%");
    }

}

    @Override
    protected void onDestroy() {
    unregisterReceiver(batteryReceiver);
    super.onDestroy();
 }
}    

then in onReceive() ,you can arg1.getIntExtra("",0) ,the first parmas can like these:

  • "level" (int) residual capacity
  • "scale" (int) the maximun
  • "voltage" (int) voltage
  • "temperature" (int) temperature
  • "techonology" (String) battery stype
  • "status"(int) charged state
  • etc..

Hope can help you.

Sanoop Surendran
  • 3,241
  • 3
  • 24
  • 46
zhangxiaoping
  • 230
  • 2
  • 6