Android 8.0: Java.Lang.Illegalstateexception: Not Allowed to Start Service Intent

Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent

The permitted situations are a temporary whitelist where the background service behaves the same as before Android O.

Under certain circumstances, a background app is placed on a temporary whitelist for several minutes. While an app is on the whitelist, it can launch services without limitation, and its background services are permitted to run. An app is placed on the whitelist when it handles a task that's visible to the user, such as:

  • Handling a high-priority Firebase Cloud Messaging (FCM) message.
  • Receiving a broadcast, such as an SMS/MMS message.
  • Executing a PendingIntent from a notification.
  • Starting a VpnService before the VPN app promotes itself to the foreground.

Source: https://developer.android.com/about/versions/oreo/background.html

So in other words if your background service does not meet the whitelist requirements you have to use the new JobScheduler. It's basically the same as a background service, but it gets called periodically instead of running in the background continuously.

If you're using an IntentService, you can change to a JobIntentService. See @kosev's answer below.

Android 9.0 (APi 28): java.lang.IllegalStateException: Not allowed to start service Intent

For pre Oreo devices, you have to use startService(), but from Oreo onwards devices, you have to use startForgroundService(). Check the below sample code.

   ContextCompat.startForegroundService(new Intent(context, MyService.class));

Background Execution Limits in Android Oreo. ContextCompat makes Build.Version check inside and calls right method

To show the notification to the user use below code in your service.

@Override
public void onCreate() {
super.onCreate();
startForeground(NOTIFICATION_ID,new Notification());
}

java.lang.IllegalStateException: Not allowed to start service Intent while trying to run RingtoneService

If you are running your code on Android 8.0 then this behavior is expected. Based on the documentation, starting from Android 8.0, you cannot start a service in background if your application is not in foreground. You need to replace following:

Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
context.startService(serviceIntent);

Do

Intent serviceIntent = new Intent(context, RingtonePlayingService.class);
ContextCompat.startForegroundService(context, serviceIntent );

Ensure to call startForeground() in your onHandleIntent with notification. You can refer to this SO for details to implement it.

Not allowed to start service Intent - Android Oreo

I experience the same behaviour.

Why this issue happens?

Due to the new Background Execution Limits of Android 8 and you should
not start services background.

How I fixed it

Migrate your GCMIntetService to JobIntentService instead of IntentService.

Please follow this steps:
1) Add the BIND_JOB_SERVICE permission to your service:

<service android:name=".service.GCMIntentService"
android:exported="false"
android:permission="android.permission.BIND_JOB_SERVICE"/>

2) In your GCMIntentService, instead extending the IntentService, use android.support.v4.app.JobIntentService and override onHandleWork
then remove the override in you onHandleIntent.

public class GCMIntentService extends JobIntentService {

// Service unique ID
static final int SERVICE_JOB_ID = 50;

// Enqueuing work in to this service.
public static void enqueueWork(Context context, Intent work) {
enqueueWork(context, GCMIntentService.class, SERVICE_JOB_ID, work);
}

@Override
protected void onHandleWork(@NonNull Intent intent) {
onHandleIntent(intent);
}

private void onHandleIntent(Intent intent) {
//Handling of notification goes here
}
}

Then finally, in your GCMBroadcastReceiver, enqueue your GCMIntentService.

public class GCMBroadcastReceiver extends WakefulBroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
// Explicitly specify that GcmIntentService will handle the intent.
ComponentName comp = new ComponentName(context.getPackageName(),
GCMIntentService.class.getName());
// Start the service, keeping the device awake while it is launching.
// startWakefulService(context, (intent.setComponent(comp)));

//setResultCode(Activity.RESULT_OK);

GCMIntentService.enqueueWork(context, (intent.setComponent(comp)));
}
}

This implementation work for me after we update our target sdk to 27 and I hope it works for you.

App sometimes crashes with Background start not allowed: service Intent

Android 8.0 (API level 26) and above have limitation with respect to background service. It includes the following changes to specific methods:

The startService() method now throws an IllegalStateException if an app targeting Android 8.0 tries to use that method in a situation when it isn't permitted to create background services.
The new Context.startForegroundService() method starts a foreground service. The system allows apps to call Context.startForegroundService() even while the app is in the background. However, the app must call that service's startForeground() method within five seconds after the service is created.

How to solve the error: Not allowed to start service Intent : app is in background uid?

Upon some searching I got the answer that I had to check the SDK version that I can then start it as foreground service or just with starteService;

class StartRelayServiceAtBootReceiver : BroadcastReceiver() {

override fun onReceive(context: Context, intent: Intent) {

if (Intent.ACTION_BOOT_COMPLETED == intent.action) {
val intent = Intent(context, MyService::class.java)
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(intent)
} else {
context.startService(intent)
}
Log.i("Autostart", "started")
}
}
}


Related Topics



Leave a reply



Submit