【问题标题】:Background execution not allowed. Android O pendingintent不允许后台执行。 Android O 未决意向
【发布时间】:2017-09-26 16:31:18
【问题描述】:

我有一项服务,它安排了一个启动我的通知的未决意图。但是,由于 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

```

我的未决意向:

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

【问题讨论】:

  • 已解决,稍后补充解决方案
  • 你是怎么做到的?

标签: android android-intent broadcastreceiver android-notifications android-8.0-oreo


【解决方案1】:

我通过添加前台服务解决了这个问题:

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

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

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

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

【讨论】:

  • 对我没用,甚至没有调用 onReceive 方法
  • 除了始终向用户显示通知之外,还有其他解决方案,这一定不是一个好主意?
  • @AnkitKumarSingh 请看这个答案:stackoverflow.com/a/54273840/297710 - 它是关于自我注册到所需的隐式意图。并提到,当可能需要前台服务时......
  • 这和googlequicksearchbox有什么关系?
猜你喜欢
  • 2020-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多