是否可以从此代码开始创建电池电量的永久通知?:
public void onCreate(Bundle savedInstanceState) {
...
this.registerReceiver(this.myBatteryReceiver , new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
...
}
private BroadcastReceiver myBatteryReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
//here you can use level as you wish.
}
};
这是一个通知的例子。
private void CheckNoti(){
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(
service.this);
notificationBuilder.setContentTitle("Title");
notificationBuilder.setContentText("Context");
notificationBuilder.setTicker("TickerText");
notificationBuilder.setWhen(System.currentTimeMillis());
notificationBuilder.setSmallIcon(R.drawable.ic_stat_icon);
Intent notificationIntent = new Intent(this, service.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
notificationIntent, 0);
notificationBuilder.setContentIntent(contentIntent);
notificationBuilder.setDefaults(Notification.DEFAULT_SOUND
| Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE);
mNotificationManager.notify(1,
notificationBuilder.build());
} }
如果可能,我必须创建一个带有电池电量的永久通知。我该怎么做?