在Android 7(One Plus 3T)上杀死应用程序时,警报管理器未触发

时间:2017-06-27 05:46:25

标签: android alarmmanager

自介绍打盹模式和App StandBy管理警报以来已发生变化。我面临的问题是我的报警管理器在KitKat,Lolipop和Marshmellow设备上正常启动但在API 23以上它不会触发,除非应用程序处于前台或后台。但是如果应用程序被杀死,警报就会停止。

在我的Android 7上查看了Google Keep应用程序,事实证明它也是如此。

但无论应用是否被杀,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" />

1 个答案:

答案 0 :(得分:-1)

尝试为alertIntent添加以下行 myIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); PendingIntent pendingIntent = PendingIntent.getBroadcast(this,0,myIntent,0);