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

查看:360
本文介绍了具有广播接收器和隐式广播禁止功能的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中,我无法再使用清单中定义的隐式广播.很好,给出的替代方法是像这样手动注册它:

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)的隐式广播来触发警报?有人可以通过代码示例或文档链接启发我.定位O时,Timely或任何其他闹钟应用程序如何工作?

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?

推荐答案

稍微修改代码以使广播显式而不是隐式,您就可以了(假设thisActivity引用或其他一些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天全站免登陆