6

Using Android, when I received a notification push throw my GCMIntentService, I want to know if my app is open or not, because if my app is open when the user click on the notification I want to do nothing, but if the app is close I want to open the app.

tshepang
  • 12,111
  • 21
  • 91
  • 136
jlmg5564
  • 1,380
  • 13
  • 28

3 Answers3

8

Launch the root activity (the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest) and add Intent.FLAG_ACTIVITY_NEW_TASK. If the app is already active (no matter which activity is on top) this will just bring the task to the front. If the app isn't active, it will start it with your root activity.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • 2
    But when the app is already active I don't want do nothing, I don't want bring the activity to the front – jlmg5564 May 08 '13 at 10:49
  • 1
    Yes, if the app is already active it will bring the task containing the app to the front, but it will not start the root activity again. Isn't that what you want? – David Wasser May 08 '13 at 12:27
  • 3
    No, when the app is already active (open, running in foreground) I don't want to do anything – jlmg5564 May 08 '13 at 15:05
  • If the app is already running in the foreground, this won't do anything. If it is running in the background, it will bring it to the foreground. If it isn't running it will start it. Have you tried this? – David Wasser May 08 '13 at 15:06
  • david your two comments here aren't the same thing. If any activity is open then this will bring the main activity to the front. The question is: If you're on a deeper activity (a few flows in for example) then how do you NOT go to the main activity – Sababado Oct 02 '13 at 17:39
  • 3
    @Sababado What you said is not true. If you just "launch" the root activity, it will **not** bring the root activity to the front (ie: the top of the activity stack). What this does is to bring the entire **task** (which was started with the root activity) to the foreground, in whatever state it was left in. This means that if you are 6 activities deep in your application, then you press HOME, then pull down the notification bar and select a notification that **launches the root activity of your application**, this will return that task to the foreground with the 6th level activity showing. – David Wasser Oct 02 '13 at 22:15
  • 1
    I have an application with 3 activities that uses notifications to start/resume my application. David's solution works for me if the application is started from the notification, every future notification will resume the application like i wanted, BUT if i start the application by clicking the application icon and then click the notification, it will create a new instance and i don't know why, the task id looks the same, any idea why this happens? – xlar8or Apr 10 '14 at 21:50
  • @xlar8or please ask a new question and be sure to post your manifest and the code that you use to create the notification. – David Wasser Apr 11 '14 at 08:36
  • @DavidWasser What can i do if i want to do nothing if app is not running. if app is running and error occured then i want to go for root activity. – Pratik Butani Jun 24 '14 at 07:42
  • @PratikButani In this case, you should have your notification send a broadcast `Intent` instead of starting an activity. If your app is running, it should register a listener for the broadcast `Intent`. If your app isn't running, nothing will be listening for the broadcast so nothing will happen. – David Wasser Jun 24 '14 at 08:52
1

Define this in all the activities : 1.) Define a static final boolean flag named 'check_running mode' 2.) Define(override) onResume() and onPause() methods in all activities. 3.) Set the value of this falg as 'true' in onResume() and 'false' in OnPause() methods respectively. 4.) Check out when u receive the push notification : a. If falg value is true it means app is in forground so do nothing in that case b. If flag value is false it means app is in background so You can open the app in that case

NOTE: The falg must be static final as you can change it from any of the activity and access it in your receiver class simply. I hope it'll work 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();
}
Maddy Sharma
  • 4,870
  • 2
  • 34
  • 40
-1
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;
}
krunal shah
  • 364
  • 2
  • 14