不允许后台执行. Android O待定意向

编程入门 行业动态 更新时间:2024-10-11 13:26:13
本文介绍了不允许后台执行. Android O待定意向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一项服务,该服务计划了一个挂起的意图,该意图会启动我的通知.但是,由于Android O,我收到此错误.我做了一些研究,偶然发现context.registerReceiver,但这似乎无法解决问题.

I have a service that schedules a pendingintent which starts my notification. However, since Android O I am getting this error. I did some research, and stumbled upon context.registerReceiver , but that does not seem to fix the problem.

错误:

W/BroadcastQueue: Background execution not allowed: receiving Intent { act=android.intent.action.PACKAGE_ADDED dat=package:my.great.package flg=0x4000010 (has extras) } to com.google.android.googlequicksearchbox/com.google.android.apps.gsa.googlequicksearchbox.GelStubAppWatcher

```

我的待定意图:

Intent myNotification = new Intent("services.notifications.Notification"); PendingIntent pendingIntent = PendingIntent.getBroadcast(this, (int) (Math.random() * Integer.MAX_VALUE), myNotification, PendingIntent.FLAG_UPDATE_CURRENT); AlarmManager alarmManager = (AlarmManager) (this.getSystemService(Context.ALARM_SERVICE)); alarmManager.setExact(AlarmManager.RTC_WAKEUP, day.getTimeInMillis(), pendingIntent);

我的通知:

public class Notification extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { context.registerReceiver(this, new IntentFilter()); try { WakeLock wakeLock = ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).newWakeLock(1, "NotificationWakeLock"); wakeLock.acquire(10000); try { scheduleNotification(context, intent); } finally { wakeLock.release(); } } catch (NullPointerException e) {} } }

推荐答案

我通过添加前台服务来解决它:

I resolved it by adding a foreground service:

Intent test = new Intent(this, NotificationService.class); startForegroundService(test);

这将显示一条通知,告知我的应用程序正在前台运行.

This will show a notification telling that my app is running on the foreground.

并通过在我的服务的oncreate中添加它:

And by adding this in my service's oncreate:

startForeground(100, new NotificationCompat.Builder(this).build());

更多推荐

不允许后台执行. Android O待定意向

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

发布评论

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

>www.elefans.com

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