How do I know if my application is in the foreground or in the background, Android?

I need to check if my application is running in the background or foreground, and then perform some operations regarding it.

I searched a lot and a clear solution is not available.

  • Make parent activity in their onPause () and onResume () methods so that some variable is updated accordingly. When you create any new action, you inherit the parent activity. Although this is the best solution that I feel to achieve my task, sometimes, if the power button is pressed, even if the application is in the background, onResume () is called.

  • Use GETTASKS permission. This solution is also good. But it can only be used for debugging purposes. Not if you want to host your application on the Google Play Store.

Get Running Taks

Any other preferred solutions for this?

+5
source share
6 answers

Well, this solved my problem:

private boolean isAppOnForeground(Context context,String appPackageName) { ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); if (appProcesses == null) { return false; } final String packageName = appPackageName; for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { // Log.e("app",appPackageName); return true; } } return false; } 
+5
source

use AppVisibilityDetector , I implement this class to determine the visibility status of the application. it can detect the foreground and background state and execute the callback method.

  AppVisibilityDetector.init(MyApp.this, new AppVisibilityCallback() { @Override public void onAppGotoForeground() { //app is from background to foreground } @Override public void onAppGotoBackground() { //app is from foreground to background } }); 

MyApp is your application class

 public class MyApp extends Application { ... } 

you do not need to add any other codes to your activity or any permissions in AndroidManifest.xml

+2
source

You can use this code to get the foreground status ( true ) of your application

 public boolean isAppForground(Context mContext) { ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> tasks = am.getRunningTasks(1); if (!tasks.isEmpty()) { ComponentName topActivity = tasks.get(0).topActivity; if (!topActivity.getPackageName().equals(mContext.getPackageName())) { return false; } } return true; } 
+1
source

Use the following function to check if your application is in Background or Foreground.

  public static Boolean getProcessState(Context mContext) { ActivityManager am = (ActivityManager) mContext.getSystemService(Context.ACTIVITY_SERVICE); boolean noProcessOnForeground = true; boolean isProcessForeground = false; System.out.println("Checking if process: " + mContext.getApplicationInfo().processName + " is Foreground or Background"); List<ActivityManager.RunningAppProcessInfo> current_processes = am.getRunningAppProcesses(); for (ActivityManager.RunningAppProcessInfo appProcess : current_processes) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) { noProcessOnForeground = false; if ((mContext.getApplicationInfo().processName).equalsIgnoreCase(appProcess.processName)) { isProcessForeground = true; System.out.println("Process is Foreground"); break; // Toast.makeText(getApplicationContext(), "Process is Foreground", Toast.LENGTH_SHORT).show(); } else { System.out.println("Process is Background"); // Toast.makeText(getApplicationContext(), "Process is Background", Toast.LENGTH_SHORT).show(); isProcessForeground = false; break; } } } if (noProcessOnForeground) { System.out.println("there is no process on foreground so setting " + mContext.getApplicationInfo().processName + " as background"); isProcessForeground = false; } return isProcessForeground; } 
0
source

Just want to use below code in Activity / Fragment:

 final boolean isAlive = AppVisibilityHelper.isForeground(HomeActivity.this); 

Want to make one class as shown below:

  public class AppVisibilityHelper{ public static boolean isForeground(final Context context) { final String packageName = "com.acb.android"; ActivityManager manager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningTaskInfo> runningTaskInfo = manager.getRunningTasks(1); ComponentName componentInfo = runningTaskInfo.get(0).topActivity; return componentInfo.getPackageName().equals(packageName); } } 

If you want to check it every one second, use the action below in Office:

  Runnable CheckAppIsRunning = new Runnable() { @Override public void run() { final boolean isAlive = AppVisibilityHelper.isForeground(HomeActivity.this); if (isAlive) { // App is running } else { // App is not running } } appIsRuningHandler.postDelayed(CheckAppIsRunning, 1000); } }; 

And in onCreate () just call it once:

 appIsRuningHandler.postDelayed(CheckAppIsRunning, 1000); 
0
source

check application in background state or background state

 ActivityManager activityManager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE); List<ActivityManager.RunningAppProcessInfo> appProcesses = activityManager.getRunningAppProcesses(); final String packageName = getPackageName(); for (ActivityManager.RunningAppProcessInfo appProcess : appProcesses) { if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND && appProcess.processName.equals(packageName)) { //if app in background state this will execute Intent intent = getPackageManager().getLaunchIntentForPackage("hinditextonphoto.com.allcomponents"); startActivity(intent); System.out.println("bbbbbbbbbbbbb background"); } else if (appProcess.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND && appProcess.processName.equals(packageName)) { //if app in forground state this will execute System.out.println("bbbbbbbbbbbbb forground"); } } 
0
source

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


All Articles