How to know if my application is in foreground or background, android?

偶尔善良 提交于 2019-12-31 10:04:10

问题


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

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

  1. Make a parent activity in its onPause() and onResume() methods keep some variable to update them accordingly. When you create any new activity inherit your parent activity. Although this is the best solution I feel to achieve my task, but sometimes if the power button is clicked even though application is in background, it's onResume() is invoked.

  2. Use GETTASKS permission - This solution is also good. But it can only used for debug purpose. Not if you want to put your app on Google Play Store.

Get Running Taks

Any other preferred solution for this?


回答1:


Well this solved my issue:

  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;
}



回答2:


use AppVisibilityDetector, I implement this class to detect the app visibility status. it can detect the foreground and background status and perform 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
    }
});

the MyApp is your Application class

public class MyApp extends Application { ... }

you don't need add some other codes to your Activity or any permissions in the AndroidManifest.xml




回答3:


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

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;
}



回答4:


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;
}



回答5:


Just want to use below code in Activity/Fragment:

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

Want to make one class like 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 after every one second then use below code in Activity:

 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 like:

appIsRuningHandler.postDelayed(CheckAppIsRunning, 1000);



回答6:


check app is in forground 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");

       }

  }


来源:https://stackoverflow.com/questions/26879951/how-to-know-if-my-application-is-in-foreground-or-background-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!