Error starting service at startup (ACTION_BOOT_COMPLETED)

1

I'm having a problem initiating a service on android as soon as the phone connects I followed several tutorials but it always causes this error

  

06-04 23: 56: 27.305 10948-10948 / pluswallpapers.devmarques.com.wallpapersall E / AndroidRuntime: FATAL EXCEPTION: main       Process: pluswallpapers.devmarques.com.wallpapersall, PID: 10948       java.lang.RuntimeException: Unable to start receiver pluswallpapers.devmarques.com.wallpapersall.YouWillNeverDestroy.StartServiceBoot: java.lang.IllegalStateException: Not allowed to start service Intent {flg = 0x10000000 cmp = pluswallpapers.devmarques.com.wallpapersall / .YouWillNeverDestroy .MyService}: app is in background uid UidRecord {2368819 u0a298 RCVR idle change: uncached procs: 1 seq (0,0,0)}           at android.app.ActivityThread.handleReceiver (ActivityThread.java:3216)           at android.app.ActivityThread.-wrap17 (Unknown Source: 0)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1688)           at android.os.Handler.dispatchMessage (Handler.java:106)           at android.os.Looper.loop (Looper.java:164)           at android.app.ActivityThread.main (ActivityThread.java:6520)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:440)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)        Caused by: java.lang.IllegalStateException: Not allowed to start service Intent {flg = 0x10000000 cmp = pluswallpapers.devmarques.com.wallpapersall / .YouWillNeverDestroy.MyService}: app is in background uid UidRecord {2368819 u0a298 RCVR idle change: uncached procs : 1 seq (0,0,0)}           at android.app.ContextImpl.startServiceCommon (ContextImpl.java:1521)           at android.app.ContextImpl.startService (ContextImpl.java:1477)           at android.content.ContextWrapper.startService (ContextWrapper.java:650)           at android.content.ContextWrapper.startService (ContextWrapper.java:650)           at pluswallpapers.devmarques.com.wallpapersall.YouWillNeverDestroy.StartServiceBoot.onReceive (StartServiceBoot.java:15)           at android.app.ActivityThread.handleReceiver (ActivityThread.java:3209)           at android.app.ActivityThread.-wrap17 (Unknown Source: 0)           at android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1688)           at android.os.Handler.dispatchMessage (Handler.java:106)           at android.os.Looper.loop (Looper.java:164)           at android.app.ActivityThread.main (ActivityThread.java:6520)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run (RuntimeInit.java:440)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)

public class StartServiceBoot extends BroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    if (Intent.ACTION_BOOT_COMPLETED.equals(intent.getAction())){
        Intent serviceIntent = new Intent(context, MyService.class);        
        context.startService(serviceIntent);
    }
}

}

Remembering that it's not just an activity in the background, if the service is started by an activity that works normally, I think I should be infringing something of android security not to boot into the broadcast.

    
asked by anonymous 05.06.2018 / 05:02

1 answer

2

This has to do with limitations imposed by Android 8 on running services in background . These services can only be launched if the application is in foreground

The documentation suggests that you use a foreground service ( startForegroundService() instead of startService() , or a scheduled job

This article from CommonsWare also suggests using the < a href="https://developer.android.com/reference/android/content/BroadcastReceiver.html#goAsync ()"> goAsync () .

    
05.06.2018 / 10:44