Мое приложение имеет уведомление в панели уведомлений, в котором отображается уровень заряда батареи. Уведомление работает, но когда установлен флажок в настройках и появляется уведомление, я пытаюсь вывести панель уведомлений, и она отстает. Я не знаю, как, но я думаю, что это потому, что проверка уведомление каждый второй уровень заряда батареи, но я не уверен .. я отправляю код:Уведомления отключены, когда onGoing уведомляется на
@SuppressLint("NewApi")
private void checkPref(Intent intent){
this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
SharedPreferences myPref = PreferenceManager.getDefaultSharedPreferences(MainActivity.this);
boolean pref_opt1 = myPref.getBoolean("firstDependent", false);
int level= intent.getIntExtra(BatteryManager.EXTRA_LEVEL,-1);
if (pref_opt1){
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(getApplicationContext())
.setContentTitle("Battery Informations")
.setContentText("Battery level"+" "+level+"%")
.setSmallIcon(R.drawable.icon_small_not)
//.setLargeIcon(aBitmap)
.setTicker(level+"%")
.build();
notification.flags = Notification.FLAG_ONGOING_EVENT;
Intent i = new Intent(MainActivity.this, MainActivity.class);
PendingIntent penInt = PendingIntent.getActivity(getApplicationContext(), 0 , i , 0);
notifi.notify(215,notification);
} else {
NotificationManager notifi = (NotificationManager)getSystemService(Context.NOTIFICATION_SERVICE);
notifi.cancel(215);
}
}
Может быть, проблема это один this.registerReceiver(this.batteryInfoReceiver, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
. Но я знаю только так, чтобы обновлять уведомление, когда уровень заряда батареи меняется. Некоторые помогают?
Итак, как я могу изменить свой код? Извините, это мое первое приложение :-) –