In our Application class in onCreate we have following code:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O
&& ((PowerManager) context.getSystemService(Context.POWER_SERVICE)).isIgnoringBatteryOptimizations(context.getPackageName())) {
final Intent pendingIntent= new Intent(context, ScheduledUpdateService.class);
startService(pendingIntent);
}
}
}
On the line "startService()" we get many crashes according to Firebase Crashyltics:
Caused by java.lang.IllegalStateException: Not allowed to start service Intent { ... PairActivityUserId=11 }: app is in background uid UidRecord{3ec0561 u11a247 SVC idle procs:1 seq(0,0,0)}
at android.app.ContextImpl.startServiceCommon(ContextImpl.java:1538)
at android.app.ContextImpl.startService(ContextImpl.java:1484)
at android.content.ContextWrapper.startService(ContextWrapper.java:663)
at com.myapp.app.MyApplication.onCreate(MyApplication.java:30)
at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:1125)
at android.app.ActivityThread.handleBindApplication(ActivityThread.java:6062)
at android.app.ActivityThread.-wrap1(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1764)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Until now it happens on Android 8 and 9 on different devices (Samsung, Lenovo, ZTE, etc.) Shouldn't that check be enough to start an intent from the background? Any hints?