Android 8.0 starts the service with errors: java.lang.runtimeexception and java.lang.illegalstateexception
Error message:
java.lang.RuntimeException: Unable to start receiver cn.edu.zut.broadservice.MyReceiver: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=cn.edu.zut.broadservice/.MyService }: app is in background uid UidRecord{b67c471 u0a86 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3388)
at android.app.ActivityThread.access$1200(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=cn.edu.zut.broadservice/.MyService }: app is in background uid UidRecord{b67c471 u0a86 RCVR idle change:uncached procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1577)
at android.app.ContextImpl.startService(ContextImpl.java:1532)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at android.content.ContextWrapper.startService(ContextWrapper.java:664)
at cn.edu.zut.broadservice.MyReceiver.onReceive(MyReceiver.java:24)
at android.app.ActivityThread.handleReceiver(ActivityThread.java:3379)
at android.app.ActivityThread.access$1200(ActivityThread.java:199)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1661)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Error reason:
If an application for Android 8.0 tries to use the startservice()
function without allowing it to create a background service, the function will raise a IllegalStateException
The new context. Startforeroundservice()
function will start a foreground service. Now, even if the application runs in the background, the system allows it to call context. Startforeroundservice()
. However, the application must call the startforegroup()
function of the service within five seconds after the service is created
For more information, see background execution restrictions
Solution:
In the place where the service was originally started, set startservice(); Replace with the following code
Intent intent = new Intent(context, MyService.class);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent);
} else {
context.startService(intent);
}
And add it in the class of service (this content can be ignored if it runs normally after adding the above code)
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1,new Notification());
}
Reference: reference address
Similar Posts:
- Android – Unable to add window — token null is not for an application [How to Solve]
- [Solved] Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
- java.lang.IllegalArgumentException: You cannot start a load for a destroyed activity
- Fragment Loading Error: java.lang.IllegalArgumentException: No view found for id …
- Unable to instantiate application
- How to Solve Error: Unable to instantiate activity ComponentInfo
- java.lang.IllegalArgumentException: Service Intent must be explicit: Intent { act=com.ba.cal
- Can’t create handler inside thread that has not called Looper.prepare()
- [Solved] Android Studio Warning: Default Activity not found
- Service Intent must be explicit solution: continued