How to find out if my application is open when I managed to receive a push notification

Using Android, when I received a notification, send my GCMIntentService, I want to know if my application is open or not, because if my application is open when the user clicks on the notification, I don’t want to do anything, but if app close I want to open the application .

+6
source share
4 answers

Launch root activity (one that has ACTION = MAIN and CATEGORY = LAUNCHER in the manifest) and add Intent.FLAG_ACTIVITY_NEW_TASK . If the application is already active (no matter what activity is on top), it will simply take the task forward. If the application is inactive, it will launch it with your root action.

+8
source

Define this in all actions: 1.) Define a static final logical flag named 'check_running mode' 2.) Define (override) the onResume () and onPause () methods in all actions. 3.) Set the value of this falg to "true" in onResume () and "false" in the OnPause () methods, respectively. 4.) Check when you receive a push notification: a. If the falg value is true, this means that the application is in the field, so do nothing in this case b. If the flag value is false, this means that the application is in the background, so you can open the application in this case

NOTE. Falg should be static final, as you can just change it from any activity and access it in your receiver class. Hope this works for you!

 1 : static boolean check_running mode = false; ------------------- 2: @Override protected void onResume() { super.onResume(); check_running mode = true; } @Override protected void onPause() { check_running mode = false; super.onPause(); } --------------------- 3 : if (check_running mode) { showUserView(); } else { showNotification(); } 
+1
source

I think you are looking for the flag FLAG_ACTIVITY_SINGLE_TOP.

http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_SINGLE_TOP

-1
source
 public static boolean isAppRunning(Context context) { // check with the first task(task in the foreground) // in the returned list of tasks ActivityManager activityManager = (ActivityManager) context .getSystemService(Context.ACTIVITY_SERVICE); List<RunningTaskInfo> services = activityManager .getRunningTasks(Integer.MAX_VALUE); if (services.get(0).topActivity.getPackageName().toString() .equalsIgnoreCase(context.getPackageName().toString())) { // your application is running in the background return true; } return false; } 
-1
source

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


All Articles