Android error: java.lang.illegalstateexception: not allowed to start service intent
Error resolution 12
Today’s test threw me a crash message:
Caused by: java.lang.IllegalStateException: Not allowed to start service Intent { cmp=xxx }: app is in background uid UidRecord{xxx u0a136 CEM idle procs:1 seq(0,0,0)}
Can it be hard for me
once you see it, you can see that it’s a background service startup error. Just put the service in the foreground, easy
In fact, Google took a look
https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten
The problem is that Android 8.0 does not allow the creation of background services.
Solution:
Method 1:
The simplest way is to directly open the front desk service
set the service to the front desk in oncreate of your service class
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
startForeground(1, new Notification());
}
Of course, this method will show what is running on the status bar of Android 5.0 and 6.0 phones please refer to the following method
Method 2:
The method from here
first modify the service configuration in your manifest file
<service android:name=".YourService"
android:permission="android.permission.BIND_JOB_SERVICE"/>
Then change the modification inheritance of your service class to extensions jobintentservice
and add the following code
public static final int JOB_ID = 1;
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, YourService.class, JOB_ID, work);
}
@Override
protected void onHandleWork(@NonNull Intent intent) {
// your code
}
The startup service is modified as follows.
YourService.enqueueWork(context, new Intent());
However, I tested that the content in onstartcommand() does not execute. No reason has been found
I hope you can help me with the analysis.
It’s over