3

我有一個服務,計劃一個pendingintent開始我的通知。不過,由於Android O我得到這個錯誤。我做了一些研究,並偶然發現了context.registerReceiver,但這似乎不能解決問題。不允許後臺執行。 Android O pendingintent

錯誤:

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

解決了這個問題,將在以後添加的解決方案 – Jason

+0

你是怎麼做到這一點? – NinjaCoder

回答

0

我加入了前臺的服務解決它:

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

這將顯示通知,告知我的應用程序正在前臺運行。

並通過我的服務的OnCreate添加此:

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