Android 8.0 java.lang.IllegalStateException Not allowed to start service Intent
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The release of Android 8.0 (Oreo) introduced several changes to the Android ecosystem, particularly in terms of application background execution and behavior. One notable change that developers need to be aware of is the introduction of the java.lang.IllegalStateException: Not allowed to start service Intent. This exception arises when an application attempts to start a service in a manner that is no longer permissible under Android 8.0's background execution limits.
Background Execution Limits in Android 8.0
Overview
Android 8.0 introduced stricter background execution limits to improve device battery life and user experience. These limitations apply to processes that are running in the background or are in the idle standby mode. Focusing on balancing system performance and app capabilities, Android 8.0 introduces limits on when and how background services can be started.
Key Change Areas
- Background Services: Apps cannot start background services unless the app itself is in the foreground.
- Broadcast Receivers: Implicit broadcasts to receivers in the background are largely restricted.
- Location Updates and Alarms: Changes were made to how apps can request location updates and send intents to background services.
Understanding java.lang.IllegalStateException
What Causes the Exception?
The exception java.lang.IllegalStateException: Not allowed to start service Intent typically occurs when a background app attempts to start a service without applying the correct handling procedures established by Android 8.0. This can happen if a developer tries to start a service from an inactive activity or uses a broadcast receiver to start a service directly in ways that are not allowed.
Code Example
Consider the following example where this exception might occur:
In Android 8.0, this code can trigger the IllegalStateException because the app is not in the foreground.
Resolution Strategies
- Use
startForegroundService()Method: ReplacestartService()withstartForegroundService(), but ensure thatstartForeground(int, Notification)is called in the service'sonCreate()method.
- JobScheduler: Use the
JobSchedulerAPI to perform background work. - WorkManager: A modern and convenient alternative for background jobs that chain and constrain complex tasks.
Adapting to New Background Execution Policies
Best Practices
- Foreground Services: Ensure your app uses
startForegroundService()for any critical tasks that need to run immediately in the background. - JobScheduler API: Utilize
JobSchedulerfor tasks that do not have immediate deadlines. - Broadcast Management: Refrain from using implicit broadcasts and switch to alternative solutions when needed, like
LocalBroadcastManager.
Comparative Summary
| Feature | Before Android 8.0 | On Android 8.0 and Later |
| Background Services | Start freely using context.startService() | Use context.startForegroundService() for services |
| Broadcast Receivers | Receive implicit broadcasts | Limited reception for implicit broadcasts |
| Job Scheduling | Partially external options (AlarmManager) | Use JobScheduler or WorkManager |
| Location Updates | Frequent and as needed | Restricted frequency for background apps |
Conclusion
The move to Android 8.0 brought about changes that enforce a more disciplined use of system resources, leading to better battery management and overall performance. Developers must adapt to these policies by employing newer APIs such as startForegroundService(), JobScheduler, and WorkManager to handle background tasks. Understanding these new constraints and adapting code accordingly is crucial in mitigating exceptions such as java.lang.IllegalStateException and ensuring a seamless user experience in apps updated for or targeting Android 8.0 and beyond.

