Calling startService will throw an IllegalStateException exception
startService
It can easily start aservice
Services can also run in separate processes.
But if it is called in the backgroundstartService
, then it is likely to throw a crash.
Caused by: : Not allowed to start service Intent { cmp=/.MyService }: app is in background uid UidRecord{b67c471 u0a86 RCVR idle change:uncached procs:1 seq(0,0,0)}
at (:1577)
at (:1532)
at (:664)
at (:664)
at $(:1661)
at (:106)
at (:193)
at (:6669)
at (Native Method)
at $(:493)
at (:858)
Cause of error
Android8.0
After that, the system added the backgroundService
The limitation of the application is in the background, callstartService
Will throwIllegalStateException
。
It means that in the background, calls are not allowedstartService
Start a background service, otherwise an exception will be thrown.
Solution
1. Use startForegroundService
The usage method is relatively simple, so I won't list the code here.
There are a few points to note:
- Use this method to start the foreground service and it will be displayed on the user's notification bar
- Must be in
5s
Calling service withinstartForeground
Method, otherwise it will happenANR
2. Use JobScheduler
Use JobScheduler to run tasks in the background.
Define a JobService:
public class MyJobService extends JobService { @Override public boolean onStartJob(JobParameters params) { return false; } @Override public boolean onStopJob(JobParameters params) { return false; } }
onStartJob
:
- Running in the main process requires avoiding time-consuming operations.
- Return true: means that the task is still executing
- Return false: means that the task has been executed
- After execution, you can call the jobFinished method to notify the system that the task has been completed.
onStopJob
:
- When the conditions are not met, the callback will be made
- Return true: indicates that the task is executed again when the condition is met.
- Return false: means that the task has completely ended
registerJobService
:
<service android:name=".MyJobService" android:permission=".BIND_JOB_SERVICE" />
Define aJobInfo
:
builder = new (111, new ComponentName(this, )); // todo Set the parameters of the task
Publish tasks to the system:
JobScheduler jobScheduler = (JobScheduler) getSystemService(Context.JOB_SCHEDULER_SERVICE); (());
3. Use WorkManager
WorkManager
yesJobScheduler
The upgraded version of the program and supports multiple processes, allowing tasks to be run into separate processes. For specific usage methods, please refer to the previous article:After the application exits, continue to run background tasks, and try WorkManager!
Summarize
Android 8.0
Above version, call in the backgroundstartService
Will throwIllegalStateException
Exception needs to be used in other ways instead.
There are three common methods:
-
startForegroundService
: This way will display the UI in the user's notification bar. -
JobScheduler
: Can realize backend operation tasks without perception. -
WorkManager
:Jetpack
The library inJobScheduler
Upgraded version, supports multiple processes.
The above is the detailed content of solving the problem of calling startService that will throw an IllegalStateException. For more information about startService IllegalStateException, please follow my other related articles!