[Solved] Startservice error: Process: com.example.provider, PID: 31612

Error Messages:

Process: com.example.provider, PID: 31612
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.provider/com.example.provider.MainActivity}: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.provider/.MyService }: app is in background uid UidRecord{9c06385 u0a69 TPSL idle change:idle|cached procs:1 seq(0,0,0)}
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=com.example.provider/.MyService }: app is in background uid UidRecord{9c06385 u0a69 TPSL idle change:idle|cached procs:1 seq(0,0,0)}
        at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1616)
        at android.app.ContextImpl.startService(ContextImpl.java:1571)
        at android.content.ContextWrapper.startService(ContextWrapper.java:669)
        at android.content.ContextWrapper.startService(ContextWrapper.java:669)
        at com.example.provider.MainActivity.onCreate(MainActivity.java:15)
        at android.app.Activity.performCreate(Activity.java:7817)
        at android.app.Activity.performCreate(Activity.java:7806)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)

 

Solution:

After Android 8.0, background processes are no longer allowed to start services directly through the startService method.:

startForegroundService

Read More: