当应用在Android 7(One Plus 3T)上被杀死时,警报管理器不触发 [英] Alarm Manager not firing when app is killed on Android 7 (One Plus 3T)

查看:129
本文介绍了当应用在Android 7(One Plus 3T)上被杀死时,警报管理器不触发的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从引入打ze模式和App StandBy以来,管理警报已更改。我面临的问题是我的警报管理器在KitKat,Lolipop和Marshmellow设备上正确启动,但在API 23以上的版本中,除非该应用程序位于前景或后台,否则它不会启动。但是,如果该应用被杀死,则警报将停止。

Since the introduction Doze Mode and App StandBy managing alarms have changed. The problem I'm facing is my alarm manager fires correctly on KitKat, Lolipop and Marshmellow devices but above API 23 it does not fire unless the app is in foreground or background. But if the app is killed, the alarms are stopped.

在我的Android 7上检出了Google Keep应用程序,事实证明它的作用相同。

Checked out Google Keep Application on my Android 7, turns out it does the same.

但是无论应用程序是否被杀死,Google日历都会触发。

But Google Calendar fires regardless of whether the app is killed or not.

完成一些阅读后发现警报管理器上的 setExactAndAllowWhileIdle 方法可确保打the睡模式并触发警报。

Done some reading and found out setExactAndAllowWhileIdle method on the alarm manager ensures to break the doze mode and trigger your alarm.

但它不起作用,这里有什么我想念的吗?

But it does not work, is there anything I'm missing here?

这是我的代码:

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" />


推荐答案

由于电池优化不起作用,我转向关闭特定应用程序的电池优化功能,在oneplus 3中可以正常工作。

Because of battery optimisation it is not working,i turned off the battery optimisation of particular app, it is working fine in oneplus 3.

您想以编程方式将应用程序从电池优化白名单中列出,请检查此链接stackoverflow.com/a/42651399/3752079

You wanna white-list your app from battery optimisation programatically, check this link stackoverflow.com/a/42651399/3752079

这篇关于当应用在Android 7(One Plus 3T)上被杀死时,警报管理器不触发的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆