0

In my app i get notifications and when i click them it opens my app. I want them to check when i click them if the application is open and currently running(not just in taskmanager but in front of every app, as if the user is using it when the notification came) and if running then it wont reopen the app, but just removes the notification. And if the app isnt infront then the notification click will open it. How can i check if the app is in this state?

I tried things like this but this checks if the app is in the task manager: public boolean isProcessRunning(String process) {

ActivityManager activityManager = (ActivityManager) getApplicationContext()
                    .getSystemService(ACTIVITY_SERVICE);
            List<RunningAppProcessInfo> procInfos = activityManager
                    .getRunningAppProcesses();
            for (int i = 0; i < procInfos.size(); i++) {
                if (procInfos.get(i).processName
                        .equals("com.myapp")) {
                    return true;
                }
            }
            return false;
        }
  • possible duplicate of [How to check if activity is in foreground or in visible background?](http://stackoverflow.com/questions/18038399/how-to-check-if-activity-is-in-foreground-or-in-visible-background) – rperryng Mar 13 '14 at 16:58

1 Answers1

1

Have you tried adding a boolean isActivityInForeground to the activity that's set to true in the onResume method and false in the onPause method?

The lifecycle chart: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

Update: If you have multiple activities, try this shared object method, referenced in the comments below: How to check if activity is in foreground or in visible background?

Community
  • 1
  • 1
Chris
  • 5,876
  • 3
  • 43
  • 69