3

J'ai un service qui programme un pendingintent qui commence ma notification. Cependant, depuis Android O, je reçois cette erreur. J'ai fait quelques recherches, et je suis tombé sur context.registerReceiver, mais cela ne semble pas résoudre le problème.L'exécution en arrière-plan n'est pas autorisée. Android O pendingintent

Erreur:

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 

`` `

Mon 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); 

Ma notification:

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

résolu le problème, ajoutera une solution plus tard – Jason

+0

comment avez-vous atteindre cet objectif? – NinjaCoder

Répondre

0

Je résolus en ajoutant un service de premier plan:

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

Une notification indiquera que mon application est en cours d'exécution.

Et en ajoutant ceci dans mon oncreate de service:

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