0

我被困在通知的创建中。只想在状态栏中显示电池电量(通知栏)通知已永久存在。这是代码的一部分:

private BroadcastReceiver batteryInfoReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

            int  level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);

              String[] status = {
                "Level: "+level
                 . . .
               };

             NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);

             Notification notification = new Notification(R.drawable.ic_launcher,"Level",System.currentTimeMillis());

             notification.flags = Notification.FLAG_ONGOING_EVENT;

             Intent i = new Intent(context, MainActivity.class); 

             PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);

             notification.setLatestEventInfo(getApplicationContext(), +level+"%", penInt);

             notifi.notify(215,notification);


            }
};

这是对的吗?例如在清单中还有其他要添加的内容吗?我没有指定的通知类,所以我以这种方式写了这一行:Intent i = new Intent(context, MainActivity.class);但我不知道它是否正确。

4

1 回答 1

0

是的,您需要添加:

<uses-permission android:name="android.permission.BATTERY_STATS" />

更多信息在这里:

使用 Android SDK 仅获取一次电池电量

于 2013-07-03T16:10:57.260 回答