Check if the app is running in the background

I really use this code to check if an application in 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 works well so far with Android 4.4. If now I check topAPN and conAPN , they are equal (and they are always not equal when the application is sent to the background on android <= 4.3).

Do you know how to solve this problem? Something has changed?

+6
source share
2 answers

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

+4
source

This code works for Android 4.4. I already used this method in onStop, here it is:

// application code is returned

 @Override protected void onStop() { super.onStop(); if (AppConstants.isAppSentToBackground(getApplicationContext())) { // Do what ever you want after app close simply Close session } } // Method to Check Our app is running or Not public static boolean isAppSentToBackground(final Context context) { try { ActivityManager am = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); // The first in the list of RunningTasks is always the foreground // task. RunningTaskInfo foregroundTaskInfo = am.getRunningTasks(1).get(0); String foregroundTaskPackageName = foregroundTaskInfo.topActivity .getPackageName();// get the top fore ground activity PackageManager pm = context.getPackageManager(); PackageInfo foregroundAppPackageInfo = pm.getPackageInfo( foregroundTaskPackageName, 0); String foregroundTaskAppName = foregroundAppPackageInfo.applicationInfo .loadLabel(pm).toString(); // Log.e("", foregroundTaskAppName +"----------"+ // foregroundTaskPackageName); if (!foregroundTaskAppName.equals("Your App name")) { return true; } } catch (Exception e) { Log.e("isAppSentToBackground", "" + e); } return false; } 
0
source

Source: https://habr.com/ru/post/957839/


All Articles