Issue
I went through a lot of answers for this question.But it's all about single activity..How to check whether the whole app is running in foreground or not ?
Solution
I don't understand what you want, but You can detect currently foreground/background application with ActivityManager.getRunningAppProcesses()
call.
Something like,
class ForegroundCheckTask extends AsyncTask<Context, Void, Boolean> {
@Override
protected Boolean doInBackground(Context... params) {
final Context context = params[0].getApplicationContext();
return isAppOnForeground(context);
}
private boolean isAppOnForeground(Context context) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses();
if (appProcesses == null) {
return false;
}
final String packageName = context.getPackageName();
for (RunningAppProcessInfo appProcess : appProcesses) {
if (appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) {
return true;
}
}
return false;
}
}
// Use like this:
boolean foregroud = new ForegroundCheckTask().execute(context).get();
Also let me know if I misunderstand..
UPDATE: Look at this SO question Determining the current foreground application from a background task or service fore more information..
Thanks..
Answered By - user370305
Answer Checked By - David Goodson (JavaFixing Volunteer)