【问题标题】:Oreo: Alarm is not fired when Application is not runningOreo:应用程序未运行时不会触发警报
【发布时间】:2018-01-11 10:17:54
【问题描述】:

我有一个相对简单的设置,应该在一天中的特定时间触发警报并向用户显示通知。这里是相关代码,

设置闹钟

 long inTime = /*expirationTime*/ Calendar.getInstance().getTimeInMillis() + 10000;
 Intent startIntent = new Intent("parking.event");
 startIntent.setClass(getBaseContext(), ParkingExpirationWarmingBroadcast.class);
 PendingIntent startPendingIntent = PendingIntent.getBroadcast(this, 99, startIntent, 0);


 alarmMgr.setExact(AlarmManager.RTC_WAKEUP,
                inTime,
                startPendingIntent);

BroadcastReceiver 已注册

    <receiver
      android:name=".modules.parking.ParkingExpirationWarmingBroadcast"
      android:enabled="true">
        <intent-filter>
            <action android:name="parking.event" />
        </intent-filter>
    </receiver>

广播接收器

class ParkingExpirationWarmingBroadcast : BroadcastReceiver() {

    @SuppressLint("NewApi")
    override fun onReceive(context: Context, intent: Intent) {
    }
}

只有在应用程序处于后台时才会触发接收器。一旦我从多任务处理中滑动应用程序,通知就会被清除,并且不会触发新的警报。我在 Android 7.0 上检查了此设置,无论应用程序是否运行,都会触发 BroadcastReceiver。

我知道 Android Oreo 中对隐式广播的限制,但我不认为我上面提到的意图被认为是隐式的。

谁能指出我做错了什么?

【问题讨论】:

    标签: android android-alarms android-8.0-oreo


    【解决方案1】:

    这是任何Android 版本的一般行为。如果您强制退出应用程序,则其AlarmsPendingIntents 也会被删除。

    herehere 可以找到相同的答案。

    【讨论】:

    • 我的理解是,从多任务处理中杀死应用程序与强制停止它不同。另外,这项工作在 7.0 上按预期工作。即使我从多任务处理中杀死应用程序也会触发警报。
    • @Ankit 在许多设备上,从recent tasks 刷一个应用程序与force quitting 它是一样的。您可能碰巧在其中一台设备上测试您的应用。
    • 我正在测试 OnePlus 3T。默认的警报应用程序似乎工作正常,即使我杀了它。让我再找一个装有奥利奥的设备。
    【解决方案2】:

    强制关闭应用程序会破坏其组件。这就是强制停止的作用。这不是一个错误,它是一个非常重要的功能。跟随下面的线程,它已经被android框架工程师讨论过了。 https://groups.google.com/forum/?fromgroups=#!topic/android-developers/anUoem0qrxU

    【讨论】:

    • 好吧,如果你阅读了这个问题,我并没有明确地从设置中强制应用程序。我刚刚从最近的应用程序中删除了它(通过按下多任务按钮并向左/向右滑动)。正如@Marko Pacak 在上面的回答中提到的那样,某些设备确实将其视为力量。我做了一些测试,问题只出在 OnePlus 设备上。
    • 其他应用程序均未按预期运行。日历不会触发事件或谷歌保持不会弹出提醒设置,如果它们不在最近的应用程序中。
    • 尝试将 setExact() 替换为 setExactAndAllowWhileIdle() 方法。它在文档中指定旧方法 setExact() 在设备进入打盹模式时不起作用,我认为这就是您的情况。关注此链接developer.android.com/training/monitoring-device-state/…
    猜你喜欢
    • 1970-01-01
    • 2014-04-06
    • 1970-01-01
    • 1970-01-01
    • 2022-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多