我想收集android中应用程序的耗电量。我在源代码中找到了 BatteryStatsHelper类,但它需要 android.permission.BATTERY_STATS 。我把它放到AndroidManifest.xml中,但它不起作用。我搜索这个问题,有人说这个权限只给系统应用程序。我有其他想法这样做。对我来说还有什么好主意?
感谢
答案 0 :(得分:-1)
你应该像这样注册广播:
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();
}
}
然后在onReceive()
,你可以arg1.getIntExtra("",0)
,第一个parmas可以喜欢这些: