I'm actually using this code to check if the app in the onPause is going to the background or not.
public static boolean isApplicationSentToBackground(final Context context) {
ActivityManager am = (ActivityManager) context.getSystemService( Context.ACTIVITY_SERVICE );
List<RunningTaskInfo> tasks = am.getRunningTasks( 1 );
if (!tasks.isEmpty()) {
ComponentName topActivity = tasks.get( 0 ).topActivity;
String name = LockScreenActivity.class.getName();
String topAPN = topActivity.getPackageName();
String conAPN = context.getPackageName();
if (topActivity.getClassName().equals( name ) || !topActivity.getPackageName().equals( context.getPackageName() )) {
return true;
}
}
return false;
}
This code has worked pretty well until now with Android 4.4. If now I check topAPN
and conAPN
they are equal (and they are always not equal when the app is sent to background on android <= 4.3).
Do you know how to solve this problem? Has something changed?