通知中的电池信息

时间:2015-02-19 12:01:44

标签: android

如何创建将电池寿命显示为百分比的程序,如通知。我创建了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 为什么以及如何得到它

1 个答案:

答案 0 :(得分:0)

尝试这样:

private BroadcastReceiver myBatteryReceiver
        = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
       int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,0);

    }

};