【发布时间】:2013-05-07 19:42:53
【问题描述】:
使用 Android,当我收到通知推送时抛出我的 GCMIntentService,我想知道我的应用程序是否打开,因为如果我的应用程序在用户单击通知时打开,我什么都不想做,但如果应用已关闭 我想打开应用。
【问题讨论】:
标签: android push-notification google-cloud-messaging
使用 Android,当我收到通知推送时抛出我的 GCMIntentService,我想知道我的应用程序是否打开,因为如果我的应用程序在用户单击通知时打开,我什么都不想做,但如果应用已关闭 我想打开应用。
【问题讨论】:
标签: android push-notification google-cloud-messaging
启动根活动(清单中包含 ACTION=MAIN 和 CATEGORY=LAUNCHER 的活动)并添加 Intent.FLAG_ACTIVITY_NEW_TASK。如果应用程序已经处于活动状态(无论哪个活动在顶部),这只会将任务带到前面。如果应用程序未处于活动状态,它将使用您的根活动启动它。
【讨论】:
在所有活动中定义这一点: 1.) 定义一个名为“check_running mode”的静态最终布尔标志 2.) 在所有活动中定义(覆盖)onResume() 和 onPause() 方法。 3.) 分别在 onResume() 和 OnPause() 方法中将此 falg 的值设置为“true”和“false”。 4.)当你收到推送通知时检查: 一种。如果 falg 值为 true,则表示应用程序处于前台,因此在这种情况下什么也不做 湾。如果标志值为 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;
}
【讨论】: