[英]How to know if my app is open when I managed a push-notification
使用Android,当我收到通知推送我的GCMIntentService时,我想知道我的应用程序是否打开,因为如果我的应用程序打开时用户点击通知我想什么都不做,但如果应用程序关闭我想打开这款应用。
Launch the root activity (the one that has ACTION=MAIN and CATEGORY=LAUNCHER in the manifest) and add Intent.FLAG_ACTIVITY_NEW_TASK
. 启动根活动(清单中具有ACTION = MAIN和CATEGORY = LAUNCHER的活动)并添加
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.
如果应用程序未处于活动状态,则会以您的根活动启动它。
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. 在所有活动中定义:1。定义名为'check_running mode'的静态最终布尔标志.2。)在所有活动中定义(覆盖)onResume()和onPause()方法。 3.) Set the value of this falg as 'true' in onResume() and 'false' in OnPause() methods respectively.
3.)分别在OnResume()和OnPause()方法中将此falg的值设置为'true',将'false'设置为'false'。 4.) Check out when u receive the push notification : a.
4.)检查何时收到推送通知:a。 If falg value is true it means app is in forground so do nothing in that case b.
如果falg值为true,则意味着app处于forground状态,因此在这种情况下不执行任何操作b。 If flag value is false it means app is in background so You can open the app in that case
如果标志值为false,则意味着应用程序处于后台,因此您可以在这种情况下打开应用程序
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. 注意:falg必须是静态final,因为您可以从任何活动中更改它并简单地在接收器类中访问它。 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();
}
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.