简体   繁体   English

如何检测用户是否正在查看我的应用(Android)

[英]How to detect if the User is looking at my app (Android)

I am trying to check to see if the user is looking at my app when it receives a c2dm message. 我试图检查接收到c2dm消息时用户是否正在查看我的应用程序。 If the user is looking at it then there is no need to generate a notification, otherwise I will generate a notification. 如果用户正在查看它,则无需生成通知,否则我将生成通知。 This seems to me like it would be quite trivial. 在我看来,这似乎是微不足道的。 However I have had a tough time trying to find a solution. 但是,我很难找到解决方案。 Any help would be greatly appreciated. 任何帮助将不胜感激。

If the user is looking at it then there is no need to generate a notification, otherwise I will generate a notification. 如果用户正在查看它,则无需生成通知,否则我将生成通知。

Have your C2DM receiver send an ordered broadcast. 让您的C2DM接收器发送有序的广播。 Have your activity (or activities) register a high-priority receiver for your broadcast, where they update themselves and abort the broadcast. 让您的一项或多项活动为您的广播注册一个高优先级的接收器,他们在此更新自己并中止广播。 Have a low-priority receiver for the broadcast registered in the manifest -- if it gets control, have it raise the Notification . 在清单中注册广播的优先级低的接收器-如果得到控制,请引发Notification

我唯一能想到的是在onResume()中设置了一些标志,通知检查该标志...然后清除标志onDestroy或onPause

final ActivityManager activityManager = (ActivityManager) getSystemService(ACTIVITY_SERVICE);
if (yourActivity.getComponentName() == activityManager.getRunningTasks(1).get(0).topActivity) {
  // Activity is currently running

}

That's one way. 那是一种方式。 Although I would probably use my activity's onStart() and onStop() to save a reference. 尽管我可能会使用活动的onStart()onStop()来保存引用。 There may be simpler ways too. 也可能有更简单的方法。

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

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