Android 9.0: Not allowed to start service: app is in background.. after onResume()

There is a workaround from Google:

The issue has been addressed in future Android release.

There is a workaround to avoid application crash. Applications can get
the process state in Activity.onResume() by calling
ActivityManager.getRunningAppProcesses() and avoid starting Service if
the importance level is lower than
ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND. If the
device hasn’t fully awake, activities would be paused immediately and
eventually be resumed again after its fully awake.

So I think it should like that:

// hack for https://issuetracker.google.com/issues/113122354
    List<ActivityManager.RunningAppProcessInfo> runningAppProcesses = activityManager.getRunningAppProcesses();
    if (runningAppProcesses != null) {
        int importance = runningAppProcesses.get(0).importance;
        // higher importance has lower number (?)
        if (importance <= ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND)
            URLPlayerService.startActionBroadcastServiceData(PlayerActivity.this);
    }

I have used handler as a workaround and it works pretty good but not 100%:

// hack for https://issuetracker.google.com/issues/113122354
   handler.postDelayed(() -> URLPlayerService.startService(PlayerActivity.this),200);

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)