
在SDK 26之前,我已经在机器人上运行了本地通知
但是在Android O中我收到了以下警告,广播接收器没有被解雇.
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中受到更多限制,但如果是这样,即使主要活动没有运行,我应该如何安排广播?
我应该使用服务而不是接收器吗?
这是AlarmManager启动代码:
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 */@OverrIDepublic voID onReceive(Context aContext, Intent aIntent){ Toast.makeText(context, text, duration).show();}}
AndroID清单:
<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是迄今为止最新的.因此,我尝试消化并提供尽可能准确的信息.
从https://developer.android.com/about/versions/oreo/background.html#broadcasts起
>针对AndroID 8.0或更高版本的应用无法再在其清单中为隐式广播注册广播接收器.
>应用程序可以在运行时使用Context.registerReceiver()为任何广播注册接收器,无论是隐式还是显式.
>应用可以继续在其清单中注册显式广播.
此外,在https://developer.android.com/training/scheduling/alarms.html中,示例使用显式广播,并未提及有关AndroID O的任何特殊内容.
我建议你尝试一下如下的明确广播吗?
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); }AlarmbroadcastReceiver
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)中启动全部内容,希望文章能够帮你解决AlarmManager和BroadcastReceiver的LocalNotification未在Android O(oreo)中启动所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)