I ran into the same problem. I solved it for new versions. Just use this code.
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; if (!topActivity.getPackageName().equals(context.getPackageName())) { return true; } } return false; }
And in the onPause method, call this function this way
@Override protected void onPause() { super.onPause(); handler.sendMessage(new Message()); } Handler handler=new Handler(new Handler.Callback() { @Override public boolean handleMessage(Message msg) { if (DeviceManager.isApplicationSentToBackground(getApplicationContext())) { paused = true; } return false; } });
I do not know the reason for excat, but maybe because of the diff stream in the handler, I m that gets the correct value
source share