如何确定是否我的活动之一是在前景 [英] How to determine if one of my activities is in the foreground

查看:131
本文介绍了如何确定是否我的活动之一是在前景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经实现其由AlarmManager触发一个BroadcastReceiver。该AlarmManager上BOOT_COMPLETED初始化。所以,我要申报清单中的接收器。

I have implemented a BroadcastReceiver which is triggered by the AlarmManager. The AlarmManager is initialized on BOOT_COMPLETED. So i have to declare the receiver in the manifest.

我的问题是,我想的BroadcastReceiver仅做东西的时候没有自己的活动是在前台(即用户不与我的应用程序交互)。我从远程服务器获取信息,并且不希望通知用户,如果他目前在我的应用程序反正。

My problem is that i want the BroadcastReceiver only to do something when none of my own activities are in the foreground (aka the user is not interacting with my application). I pull information from a remote server and don't want to notify the user if he is currently in my application anyways.

到目前为止,我还没有设法找到一种方法来确定,如果我的应用程序是在前台。有没有办法做这样的事情?该ActivityManager告诉我,如果我的应用程序正在运行,但不在于它是在前台。

So far i have not managed to find a way to determine if my application is in the foreground. Is there a way to do such thing? The ActivityManager tells me if my application is running but not whether it is in the foreground.

问题是pretty的大致相同的如下所述:<一href="http://stackoverflow.com/questions/2282435/inform-activity-from-a-broadcastreceiver-only-if-it-is-in-the-foreground">Inform从一个BroadcastReceiver的活动,只有当它是在前景

The problem is pretty much the same as described here: Inform Activity from a BroadcastReceiver ONLY if it is in the foreground

解决方法:

在评估了多个解决方案后,我想快速地说明一下,我认为是应对的背景/前景活动的最佳方法。

After evaluating several solutions i want to quickly outline what i think is the best method to deal with activities in the background/foreground.

在preferred方式是注册在活动的onResume方法的广播接收器,并注销其上的onPause活动。任何服务或其他背景的元素会比需要发送一个广播的意图与你的活动将拦截一个具体行动。

The preferred way is to register a broadcast receiver in the onResume method of your activity and to deregister it on the activities on onPause. Any service or other background element will than need to send a broadcast intent with a specific action that your activity will intercept.

如果你的活动是在前景也将有其意图接收器注册,并能直接处理的意图发送从服务。如果不是在前台它将不会接收的意图但invokved广播将知道没有截获其广播意图和将能够处理其本身的服务。例如,它可能比启动所需的活动,展现出通知等。

If your activity is in the foreground it will have its intent receiver registered and is able to directly deal with the intent send from your service. If it is not in the foreground it will not receive the intent but the service that invokved the broadcast will know that nobody intercepted its broadcast intent and will be able to deal with that itself. Eg it could than launch the desired activity, show a notification etc.

推荐答案

以下的答案:是在背景,总结了可用的背景/前景检查解决方案。

The following answer: "Is application running in background", summarizes solutions available for background/foreground checking.

注意:
previously这个答案建议使用 ActivityManager.getRunningAppProcesses(),但是这种方法似乎并不完全可靠,它的使用是不鼓励。检查上面的链接了解详细情况。

Note:
Previously this answer suggested to use ActivityManager.getRunningAppProcesses(), however that method appeared to be not completely reliable and its usage is discouraged. Check the link above for the details.

这篇关于如何确定是否我的活动之一是在前景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆