背景执行不允许。Android O中的PendingIntent。

11

我有一个服务,它安排了一个待处理意图来启动我的通知。但是,自从Android O以来,我遇到了这个错误。我进行了一些研究,偶然发现了context.registerReceiver,但似乎并没有解决问题。

错误:

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

我的PendingIntent:

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) {}
    }
}

已解决,稍后会添加解决方案。 - Jason
你是如何实现这个的? - NinjaCoder
1个回答

0
我通过添加前台服务来解决它:
Intent test = new Intent(this, NotificationService.class);
startForegroundService(test);

这将显示一个通知,告知我的应用正在前台运行。

并通过在我的服务的oncreate中添加此内容:

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

2
对我来说没用,onReceive方法甚至都没有被调用。 - fillobotto
除了一直向用户显示通知,这肯定不是一个好主意,还有其他的解决方案吗? - Ankit Kumar Singh
@AnkitKumarSingh 请查看此答案:https://dev59.com/BVUL5IYBdhLWcg3wopWy#54273840 - 它是关于自我注册到所需的隐式意图。并提到了前台服务可能需要的情况... - yvolk
那跟 googlequicksearchbox 有什么关系? - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接