Cannot start activity from Service in Android 11 devices

Viewed 305

I got this issue when I tried to start an activity from a service in android 11 devices only:

    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.android.server.wm.ActivityRecord.isVisible()' on a null object reference
        at android.os.Parcel.createExceptionOrNull(Parcel.java:2379)
        at android.os.Parcel.createException(Parcel.java:2357)
        at android.os.Parcel.readException(Parcel.java:2340)
        at android.os.Parcel.readException(Parcel.java:2282)
        at android.app.IActivityTaskManager$Stub$Proxy.startActivity(IActivityTaskManager.java:3696)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1723)
        at android.app.ContextImpl.startActivity(ContextImpl.java:1023)
        at android.app.ContextImpl.startActivity(ContextImpl.java:994)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:403)

Even though I had the System_alert_window permission which was granted by me, it threw an error above. I checked Settings.canDrawOverlays(context) before start an activity:

if (Settings.canDrawOverlays(context)) {
     Intent intent1 = new Intent(context, MyActivity.class);                               
     intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_FROM_BACKGROUND);
     context.startActivity(intent1);
}

This is the docs: https://developer.android.com/guide/components/activities/background-starts#exceptions. Does anyone have a solution for this?

0 Answers
Related