带有广播接收器和隐式广播禁止的 Android 8.0 Oreo AlarmManager [英] Android 8.0 Oreo AlarmManager with broadcast receiver and implicit broadcast ban

查看:29
本文介绍了带有广播接收器和隐式广播禁止的 Android 8.0 Oreo AlarmManager的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有通过闹钟管理器设置的重要提醒(它的功能应该与闹钟应用程序相同).

I have critical reminders that are set via the Alarm Manager (It should function the same way as an alarm clock application).

以前我的 Android 清单中有以下内容:

Previously I had the following in my Android Manifest:

    <receiver android:name="com.example.app.AlarmReceiver" >
        <intent-filter>
            <action android:name="${packageName}.alarm.action.trigger"/>
        </intent-filter>
    </receiver>

广播接收器:

public class AlarmReceiver extends BroadcastReceiver {

  @Override public void onReceive(
      final Context context,
      final Intent intent) {
// WAKE LOCK
// BUILD NOTIFICATION etc...
  }

}

如何设置闹钟:

final PendingIntent operation = PendingIntent.getBroadcast(
          mContext,
          requestCode,
          intent,
          PendingIntent.FLAG_CANCEL_CURRENT);

      if (PlatformUtils.hasMarshmallow()) {
        alarmManager.setExactAndAllowWhileIdle(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);

      } else {
        alarmManager.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, operation);
      }
}

在 Android 8.0 中,我无法再使用 Manifest 中定义的隐式广播.没关系,给出的替代方法是像这样手动注册它:

With Android 8.0 I can no longer use an implicit broadcast as defined in the Manifest. That's fine, the alternative given is to register it manually like so:

final BroadcastReceiver receiver = new AlarmReceiver();
final IntentFilter intentFilter = new IntentFilter(ALARM_RECEIVER_INTENT_TRIGGER);
context.registerReceiver(receiver, intentFilter);

这对我来说似乎不合逻辑.

This does not seem logical to me.

  1. 警报接收器将与上下文的生命周期相关联.当应用程序因内存压力或设备重新启动而被终止时,这会导致问题.我需要每次都触发警报,因为它们对用户的健康至关重要.

  1. The alarm receiver will be tied to the lifetime of the context. This causes an issue when say the application is killed due to memory pressure or when the device is restarted. I need my alarms to fire every time as they are critical for the health of the user.

即使我听 "android.intent.action.BOOT_COMPLETED" 并注册我的警报接收器,该应用程序很快就会被终止,并且不会触发任何警报.我也没有通过

Even if I listen to "android.intent.action.BOOT_COMPLETED" and register my alarm receiver the app is killed shortly after and no alarm is fired. I also don't see my alarm via

adb shell dumpsys 报警

adb shell dumpsys alarm

在面向 Android O (8.0) 时,如何创建接收隐式广播以触发警报的自定义广播接收器?有人可以通过代码示例或文档链接来启发我.Timely 或任何其他闹钟应用在面向 O 时如何运作?

How do I create a custom broadcast receiver that receives an implicit broadcast to fire an alarm while targeting Android O (8.0)? Can someone enlighten me with a code example or link to documentation. How does Timely or any other alarm clock app function while targeting O?

推荐答案

稍微修改你的代码,使广播显式而不是隐式,你会没事的(假设 this 是一个 Activity 引用或其他一些Context):

Revise your code slightly to make the broadcast explicit rather than implicit and you'll be fine (assuming this is an Activity reference or some other Context):

Intent intent = new Intent(ALARM_RECEIVER_INTENT_TRIGGER);
intent.setClass(this, AlarmReceiver.class);

Oreo 中的限制是隐式广播Intent 注册,也就是说,您向它发送广播只会指定动作、类别或数据.您可以通过指定接收广播的类来使其成为显式广播.

The restriction in Oreo is on implicit broadcast Intent registration, which is to say it you are sending it broadcasts will only action, category, or data specified. You make it an explicit broadcast by specifying the class which is to receive the broadcast.

这篇关于带有广播接收器和隐式广播禁止的 Android 8.0 Oreo AlarmManager的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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