当我管理推送通知时,如何知道我的应用是否已打开

时间:2013-05-07 19:42:53

标签: android push-notification google-cloud-messaging

使用Android,当我收到通知推送我的GCMIntentService时,我想知道我的应用程序是否打开,因为如果我的应用程序打开时用户点击通知我想什么都不做,但是如果app很接近我想打开应用程序。

3 个答案:

答案 0 :(得分:8)

启动根活动(清单中具有ACTION = MAIN和CATEGORY = LAUNCHER的活动)并添加Intent.FLAG_ACTIVITY_NEW_TASK。如果应用程序已经处于活动状态(无论哪个活动位于顶部),这只会将任务置于最前面。如果应用程序未处于活动状态,则会以您的根活动启动它。

答案 1 :(得分:1)

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

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

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();
}

答案 2 :(得分:-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;
}
相关问题