1

Following crash is reported on Android 11 for Pixel devices.

Fatal Exception: java.lang.RuntimeException Unable to start activity ComponentInfo{}: java.lang.IllegalStateException: Not allowed to start service Intent {}: app is in background uid UidRecord{}

This is my code where above exception is pointing.

   val stickyService = Intent(this, StickyService::class.java)
   startService(stickyService)

My StickyService class extends Service class which overrides following methods.

    public class StickyService extends Service {

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    return START_STICKY;
}

@Nullable
@Override
public IBinder onBind(Intent intent) {
    return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
    **Update value in preferences here**
}}

This is happening on Android 11 devices only. Is there major change in Service implementation in Android 11 from Google?

Shrikant
  • 1,560
  • 1
  • 15
  • 32
  • Does this answer your question? [Android 8.0: java.lang.IllegalStateException: Not allowed to start service Intent](https://stackoverflow.com/questions/46445265/android-8-0-java-lang-illegalstateexception-not-allowed-to-start-service-inten) – mike47 Feb 15 '23 at 02:28

2 Answers2

0

Try using foreground service, as background service is not allowed > android 8 when app is killed

  • 1
    The restrictions on the Service in Android 11 is the same as previous versions, to start the service when the application is in the background, the service should be started as a foregroundService and the application should display a notification for the foreground service,https://developer.android.com/guide/components/foreground-services – Vijay Patole Oct 26 '20 at 09:45
  • How do we use foreground service? What needs to be changed? – Shrikant Nov 05 '20 at 08:36
0

There was change with application visibility in Android 11.

If applicationA is using service from applicationB, you need to declare it in AndroidManifest.xml of applicationA:

<manifest package="com.exampleA.applicationA">
    <queries>
        <package android:name="com.exampleB.applicationB" />
    </queries>
    ...
</manifest>

So android will make visible applicationB to applicationA.

Source: https://developer.android.com/training/package-visibility/declaring

hexFlex
  • 11
  • 1
  • 2