带有 AlarmManager 和 BroadcastReceiver 的 LocalNotification 未在 Android O (oreo) 中启动

编程入门 行业动态 更新时间:2024-10-23 16:27:11
本文介绍了带有 AlarmManager 和 BroadcastReceiver 的 LocalNotification 未在 Android O (oreo) 中启动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的本地通知在 SDK 26 之前的 android 上运行

但在 Android O 中,我收到以下警告,并且广播接收器没有被触发.

But in a Android O I've got the following warning, and the broadcast receiver is not fired.

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=package.name.action.LOCAL_NOTIFICATION cat=[com.category.LocalNotification] flg=0x14 (has extras) } to package.name/com.category.localnotifications.LocalNotificationReceiver

据我所知,android O 中的广播接收器受到更多限制,但如果是这样,即使主要活动未运行,如果我希望广播也启动,我应该如何安排广播?

From what I've read broadcast receivers are more restricted in android O, but if so, how should I schedule the broadcast if I want it launching even if the main activity is not running?

我应该使用服务而不是接收器吗?

Should I use services instead of receivers?

这是AlarmManager启动代码:

This is the AlarmManager launch code:

public void Schedule(String aID, String aTitle, String aBody, int aNotificationCode, long aEpochTime) { Bundle lExtras = new Bundle(); lExtras.putInt("icon", f.getDefaultIcon()); lExtras.putString("title", aTitle); lExtras.putString("message", aBody); lExtras.putString("id", aID); lExtras.putInt("requestcode", aNotificationCode); Intent lIntent = new Intent(LocalNotificationScheduler.ACTION_NAME) .addCategory(NotificationsUtils.LocalNotifCategory) .putExtras(lExtras); PendingIntent lPendIntent = PendingIntent.getBroadcast(f.getApplicationContext(), aNotificationCode, lIntent, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager lAlarmMgr = (AlarmManager) f.getSystemService(Context.ALARM_SERVICE); lAlarmMgr.set(AlarmManager.RTC, 1000, lPendIntent); }

这是接收方代码:

public class LocalNotificationReceiver extends BroadcastReceiver { public static native void nativeReceiveLocalNotification (String aID, String aTitle, String aMessage, boolean aOnForeground ); /** This method receives the alarms set by LocalNotificationScheduler, * notifies the CAndroidNotifications c++ class, and (if needed) ships a notification banner */ @Override public void onReceive(Context aContext, Intent aIntent) { Toast.makeText(context, text, duration).show(); }

}

Android 清单:

Android manifest:

<receiver android:name="com.category.localnotifications.LocalNotificationReceiver"> <intent-filter> <action android:name="${applicationId}.action.LOCAL_NOTIFICATION" /> <category android:name="com.category.LocalNotification" /> </intent-filter> </receiver>

推荐答案

Android O 是最新的.因此,我尝试消化并提供尽可能准确的信息.

Android O are pretty new to-date. Hence, I try to digest and provide as accurate possible information.

来自 developer.android/about/versions/oreo/background.html#broadcasts

  • 面向 Android 8.0 或更高版本的应用无法再在其清单中为隐式广播注册广播接收器.
    • 应用程序可以在运行时使用 Context.registerReceiver() 为任何广播注册接收器,无论是隐式还是显式.
    • Apps that target Android 8.0 or higher can no longer register broadcast receivers for implicit broadcasts in their manifest.
      • Apps can use Context.registerReceiver() at runtime to register a receiver for any broadcast, whether implicit or explicit.

      此外,在 developer.android/training/scheduling/alarms.html ,这些示例使用了显式广播,并且没有提到关于 Android O 的任何特殊内容.

      Also, in developer.android/training/scheduling/alarms.html , the examples are using explicit broadcast, and doesn't mention anything special regarding Android O.

      我可以建议您尝试如下显式广播吗?

      May I suggest you try out explicit broadcast as follow?

      public static void startAlarmBroadcastReceiver(Context context, long delay) { Intent _intent = new Intent(context, AlarmBroadcastReceiver.class); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, _intent, 0); AlarmManager alarmManager = (AlarmManager)context.getSystemService(Context.ALARM_SERVICE); // Remove any previous pending intent. alarmManager.cancel(pendingIntent); alarmManager.set(AlarmManager.RTC_WAKEUP, System.currentTimeMillis() + delay, pendingIntent); }

      警报广播接收器

      public class AlarmBroadcastReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { } }

      在AndroidManifest中,定义类为

      <receiver android:name="org.yccheok.AlarmBroadcastReceiver" > </receiver>

更多推荐

带有 AlarmManager 和 BroadcastReceiver 的 LocalNotification 未在 Android O (oreo) 中启动

本文发布于:2023-11-27 04:29:22,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1636595.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:BroadcastReceiver   AlarmManager   LocalNotification   oreo   Android

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!