Our application has a service that usually starts during Application.OnCreate(direct call context.startService) and also later AlarmManager(the relay is in the process of transferring some of its work to the JobScheduler).
Our application also has BroadcastReceiverone that launches with its direct intention.
Given the new restrictions in Android Oreo ( https://developer.android.com/about/versions/oreo/android-8.0-changes.html ), we have a problem:
- application / process is in background / dead
- BroadcastReceiver launches OS
- Application.onCreate () is executed before BroadcastReceiver
- Application.onCreate () code is trying to start a service
this crashes with "IllegalStateException: not allowed to trigger a service intent."
I know about the new recommended ways to start the Service, as CommonsWare reports about it here https://stackoverflow.com/a/166269/2126321 , but for this particular case I just want to have one if(process in foreground) { startService }. I am currently using the following method and it seems to work:
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean isProcessInForeground_V21(Context context) {
ActivityManager am = (ActivityManager) context.getSystemService(ACTIVITY_SERVICE);
List<ActivityManager.AppTask> tasks = am.getAppTasks();
return tasks.size() > 0;
}
But I can’t find the exact checks that Android Oreo does (I got to https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/app/ContextImpl.java on startServiceCommon, but from there the flag requireForegroundseems to go over to some own implementation)
So my question is:
Android Oreo, , startService?