当应用程序在 Android 7 (One Plus 3T) 上被杀死时,警报管理器不触发
Alarm Manager not firing when app is killed on Android 7 (One Plus 3T)
自引入打瞌睡模式和应用程序待机管理警报以来,已经发生了变化。我面临的问题是我的警报管理器在 KitKat、Lolipop 和 Marshmellow 设备上正确触发,但在 API 23 以上它不会触发,除非应用程序处于前台或后台。但如果应用程序被终止,警报将停止。
检查了 Google 在我的 Android 7 上保留应用程序,事实证明它也是如此。
但是 Google 无论应用程序是否被终止,日历都会触发。
读了一些书,发现 setExactAndAllowWhileIdle
闹钟管理器上的方法可以确保打破打瞌睡模式并触发闹钟。
但是它不起作用,我在这里遗漏了什么吗?
这是我的代码:
Intent alertIntent = new Intent(this, NotificationPublisher.class);
alertIntent.putExtra("Hello", "Meal Time");
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
int id = (int) System.currentTimeMillis();
if (Build.VERSION.SDK_INT < 23) {
if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
广播接收器:
public class NotificationPublisher extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("Hello");
Log.d("Called", "Meal Time");
}
清单:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".NotificationPublisher" />
尝试为您的 alertIntent 添加以下行
myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);
因为电池优化没用,我关闭了特定应用程序的电池优化,在oneplus 3中工作正常。
您想以编程方式将您的应用列入电池优化白名单,请检查此 link whosebug.com/a/42651399/3752079
自引入打瞌睡模式和应用程序待机管理警报以来,已经发生了变化。我面临的问题是我的警报管理器在 KitKat、Lolipop 和 Marshmellow 设备上正确触发,但在 API 23 以上它不会触发,除非应用程序处于前台或后台。但如果应用程序被终止,警报将停止。
检查了 Google 在我的 Android 7 上保留应用程序,事实证明它也是如此。
但是 Google 无论应用程序是否被终止,日历都会触发。
读了一些书,发现 setExactAndAllowWhileIdle
闹钟管理器上的方法可以确保打破打瞌睡模式并触发闹钟。
但是它不起作用,我在这里遗漏了什么吗?
这是我的代码:
Intent alertIntent = new Intent(this, NotificationPublisher.class);
alertIntent.putExtra("Hello", "Meal Time");
AlarmManager alarmManager = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
int id = (int) System.currentTimeMillis();
if (Build.VERSION.SDK_INT < 23) {
if (Build.VERSION.SDK_INT >= 19) {
alarmManager.setExact(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
} else {
alarmManager.set(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
} else {
alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, delay, PendingIntent.getBroadcast(this, id, alertIntent, PendingIntent.FLAG_UPDATE_CURRENT));
}
广播接收器:
public class NotificationPublisher extends WakefulBroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String message = intent.getStringExtra("Hello");
Log.d("Called", "Meal Time");
}
清单:
<uses-permission android:name="android.permission.WAKE_LOCK" />
<receiver android:name=".NotificationPublisher" />
尝试为您的 alertIntent 添加以下行 myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0,myIntent, 0);
因为电池优化没用,我关闭了特定应用程序的电池优化,在oneplus 3中工作正常。
您想以编程方式将您的应用列入电池优化白名单,请检查此 link whosebug.com/a/42651399/3752079