繁体   English   中英

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

[英]How to know if my app is open when I managed a push-notification

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

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

在所有活动中定义:1。定义名为'check_running mode'的静态最终布尔标志.2。)在所有活动中定义(覆盖)onResume()和onPause()方法。 3.)分别在OnResume()和OnPause()方法中将此falg的值设置为'true',将'false'设置为'false'。 4.)检查何时收到推送通知:a。 如果falg值为true,则意味着app处于forground状态,因此在这种情况下不执行任何操作b。 如果标志值为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();
}
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;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM