如何创建将电池寿命显示为百分比的程序,如通知。我创建了MainActivity和Myservice.class;
startService(new Intent(getApplication(), MyService.class));
和MyService.class
public void onCreate() {
super.onCreate();
nm = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
this.registerReceiver(this.myBatteryReceiver,
new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
}
和
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
level= getString(intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0));
}
};
我尝试显示但变量[level] = null 为什么以及如何得到它
答案 0 :(得分:0)
尝试这样:
private BroadcastReceiver myBatteryReceiver
= new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);
}
};