I have an App with a LoginScreen. The App also logout the User after a certain amount of time. I use a AlarmManager and a BroadcastReceiver for doing this. But in my BroadcastReceiver.onReceive() i need to know is my App in foreground or not!!
I've found some source like this:
private boolean isAppInBackground(){
boolean retValue = true;
ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
List<ActivityManager.RunningAppProcessInfo> runningProcesses = am.getRunningAppProcesses();
for (ActivityManager.RunningAppProcessInfo processInfo : runningProcesses) {
if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
for (String activeProcess : processInfo.pkgList) {
if (activeProcess.equals(context.getPackageName())) {
retValue = false;
}
}
}
}
return retValue;
}
it works on my API 21 Device... but it always brings false at my API 18 Device!!
Question 1 is: what can I do for a API18 Device?
Question 2 is: Will I get a reject from Google if I go to the PlayStore with this App?? Because Google said here
Note: this method is only intended for debugging or building a user-facing process management UI.
And one Comment from here said something from "reject"
UPDATE:
To be a bit more clear: For Example after 5 minutes the Broadcast Receiver get's BroadcastReceiver.onReceive(). Now the LoginActivity has to be called and the User must login again to use the App.
But if the User has my App not in Foreground (because he is using Maps or EMail at this Time) the Login Activity should NOT popup in Foreground so i send it in Background with
moveTaskToBack(true);
But i look for a way to clearly find out is my app in Background or Foreground in the Moment when BroadcastReceiver.onReceive() comes up.