我正在开发省电应用程序。我需要检测剩余电池。我已经完成了以下操作。
public class HomeActivity extends Activity {
TextView tv_battery;
@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.main);
tv_battery = (TextView) findViewById(R.id.tv_battery);
}
@Override
protected void onResume() {
super.onResume();
registerReceiver(mBatteryReceiver, new IntentFilter(Intent.ACTION_BATTERY_LOW ));
}
private BroadcastReceiver mBatteryReceiver = new BroadcastReceiver(){
@Override
public void onReceive(Context ctxt, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
tv_battery.setText("Remaining Battery : "+String.valueOf(level) + "%");
}
};
}
但我没有得到电池价值。请帮我