2

I have the solution.

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;

}

It works fine.

How I can to check that my application really in background? For example, I opened activity and started new Activity - standard Gallery (started from my application). This intent was started from my activity, and my application logically in foreground. I can check my activities, but how to check this situation too?

AND how can I check that the application was sent to the background FROM external activity (on this example - gallery)?

monyag
  • 800
  • 3
  • 9
  • 27
  • Do you check from a service? – Hoan Nguyen Mar 18 '13 at 23:11
  • I think that service is no good idea... I check it from Activity onPause method – monyag Mar 19 '13 at 18:44
  • Why do you need to check? If you check from your activity, then obviously the activity is still in foreground for the code to run. Your activity may be in the process of being destroy, but your on pause code would lead you to believe it being sent to background. – Hoan Nguyen Mar 19 '13 at 18:51
  • SignOut after opening activity from background state, not more. Start application - login - send to background - restore from background - autosignout and go to the login window – monyag Mar 19 '13 at 19:25
  • What happens when login is successful? login success --> launch new activiy? – Hoan Nguyen Mar 19 '13 at 19:34
  • Yes, new activity with FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_NEW_TASK. If I use only my application (without gallery or camera) - all works fine. But if I runned the gallery intent - code in my onPause method returned topActivity == (for example, in Amazon Kindle) com.cooliris.media.Gallery. Logically my application does not sent to background, because gallery runned from my application as startActivityForResult. – monyag Mar 19 '13 at 19:48
  • So if onCreate is not called but only onResume then you want to autosignout and go to the login window? – Hoan Nguyen Mar 19 '13 at 19:58
  • no, signout only for onPause Method. Start new SignIn activity. after restoring from background I see SignInWindow. – monyag Mar 19 '13 at 20:07
  • I do not quite understand you. Do you want to go back to the SignInWindow after coming back from gallery or camera? – Hoan Nguyen Mar 19 '13 at 21:47
  • This requiers permisson in manifest: – MSquare Jul 16 '13 at 11:06
  • This requires permission in manifest: Users might not be too happy about such powerful permission. – MSquare Jul 16 '13 at 11:15
  • See this http://stackoverflow.com/questions/3667022/android-is-application-running-in-background – peceps Dec 24 '13 at 09:14

1 Answers1

1
public boolean isApplicationSentToBackground(final Context context) {

  Boolean flag = false;
  ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
  if (Build.VERSION.SDK_INT >= 23) {
    List < ActivityManager.RunningTaskInfo > tasks = am.getRunningTasks(1);
    if (!tasks.isEmpty()) {
      ComponentName topActivity = tasks.get(0).topActivity;
      if (!topActivity.getPackageName().equals(context.getPackageName())) {
        flag = true;
      }
    }
  } else {
    try {
      Thread.sleep(1500);
      flag = false;
      List < ActivityManager.RunningAppProcessInfo > runningProcesses = am.getRunningAppProcesses();
      for (ActivityManager.RunningAppProcessInfo processInfo: runningProcesses) {
        if (processInfo.importance == ActivityManager.RunningAppProcessInfo.IMPORTANCE_BACKGROUND) {
          for (String activeProcess: processInfo.pkgList) {
            if (activeProcess.equals(context.getPackageName())) {
              flag = true;
            }
          }
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
  return flag;
}
Krzysztof Janiszewski
  • 3,763
  • 3
  • 18
  • 38
Monika Moon
  • 188
  • 3
  • 9