6

使用Android,当我收到通知推送时抛出我的GCMIntentService,我想知道我的应用程序是否打开,因为如果我的应用程序在用户单击通知时打开,我什么都不想做,但如果应用程序关闭我想打开应用程序。

4

3 回答 3

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.

于 2013-05-07T21:38:51.960 回答
1

在所有活动中定义: 1.) 定义一个名为“check_running mode”的静态最终布尔标志 2.) 在所有活动中定义(覆盖)onResume() 和 onPause() 方法。3.) 分别在 onResume() 和 OnPause() 方法中将此 falg 的值设置为“true”和“false”。4.) 当您收到推送通知时结帐:如果 falg 值为 true,则表示应用程序处于前台,因此在这种情况下不执行任何操作 b。如果标志值为 false,则表示应用程序处于后台,因此您可以在这种情况下打开应用程序

注意: falg 必须是静态最终的,因为您可以从任何活动中更改它并在您的接收器类中简单地访问它。我希望它对你有用!

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();
}
于 2013-07-12T06:01:49.523 回答
-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;
}
于 2015-07-24T12:55:41.770 回答