如何知道我的 Android 应用程序是否可见?
我有一个计时器,它在结束时启动通知。但我想仅在应用程序当前不可见时使用 notificationManager 触发通知,并在应用程序位于前台时计时器结束时显示警报对话框。
我已经尝试过这个:
ActivityManager actMngr = (ActivityManager) ValeoMobileApplication.getContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
Tools.log("TimerBroadcastReceiver", "onReceive", "All running processes are listed below :");
for (RunningAppProcessInfo pi : runningAppProcesses) {
//Check pi.processName and do your stuff
//also check pi importance - check if process is in foreground or background
Tools.log("TimerBroadcastReceiver", "onReceive", pi.processName + " importance = "+pi.importance);
if(pi.processName.equalsIgnoreCase("MY_APP_PROCESS_NAME")){
if (pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
isApplicationInForeground = true;
}
}
}
但似乎应用程序是否处于前台并不重要。我该怎么做?
I have a timer that start a notification when it ends. But I would like to fire a notification using notificationManager only if the app is not currently visible, and to show an alertDialog if the timer ends while the app is in foreground.
I've already tried with this :
ActivityManager actMngr = (ActivityManager) ValeoMobileApplication.getContext().getSystemService(Activity.ACTIVITY_SERVICE);
List<RunningAppProcessInfo> runningAppProcesses = actMngr.getRunningAppProcesses();
Tools.log("TimerBroadcastReceiver", "onReceive", "All running processes are listed below :");
for (RunningAppProcessInfo pi : runningAppProcesses) {
//Check pi.processName and do your stuff
//also check pi importance - check if process is in foreground or background
Tools.log("TimerBroadcastReceiver", "onReceive", pi.processName + " importance = "+pi.importance);
if(pi.processName.equalsIgnoreCase("MY_APP_PROCESS_NAME")){
if (pi.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
isApplicationInForeground = true;
}
}
}
But it seems that it doesn't matter if the app is foreground or not. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
使用有序广播,如果 Activity 位于前台,则该 Activity 具有高优先级接收器;如果 Activity 不在前台,则具有清单注册的接收器。
这是一篇博客文章概述了此技术。
更新2018-01-04:上述方法有效,但它涉及系统广播,这对于大多数(单进程)应用程序来说不是一个很好的选择。可以使用事件总线(
LocalBroadcastManager
,greenrobot 的 EventBus)来代替,具有更好的性能和安全性。请参阅此使用LocalBroadcastManager
的示例应用 和 这个使用 greenrobot 的 EventBus 的示例应用程序,两者都实现了此模式。Use an ordered broadcast, with the activity having a high-priority receiver if it is in the foreground, plus a manifest-registered receiver for the cases when it is not in the foreground.
Here is a blog post outlining this technique.
UPDATE 2018-01-04: The approach described above works, but it involves system broadcasts, which is not a great choice for most (single-process) apps. An event bus (
LocalBroadcastManager
, greenrobot's EventBus) can be used instead, with better performance and security. See this sample app that usesLocalBroadcastManager
and this sample app that uses greenrobot's EventBus, both of which implement this pattern.您可以通过以下方式检测您的应用程序是否可见:
在您的所有
Activity
中,设置:(这可能会导致您为实现此行为的所有 Activity 定义一个超类)
然后创建a
VisibilityManager
(这是非常基本的,您可能需要更高级的东西):然后,在您的计时器线程中,当倒计时达到零时:
编辑:但我什至更喜欢此处描述的 CommonsWare 方法此页。它更加优雅。
You can detect whether your app is visible or not the following way:
In all your your
Activity
, set:(this may lead you to define a superclass to all your activities that would implement this behaviour)
Then create a
VisibilityManager
(this one is very basic, you may need something more advanced):And then, in your timer thread, when the countdown reached zero:
EDIT: but I even prefer the CommonsWare's approach described here in this page. It is more elegant.
在这种情况下,我建议您使用 Service 而不是 Activity。
服务在后台运行,如果正确启动,则不受 Activity 生命周期的影响。
要记住的一件重要的事情是,当您返回 UI 时,服务必须显式调用 UI 线程,否则您将收到 ANR 错误,因为 UI 线程不是线程安全的。
请通读该文档,它应该可以帮助您为您想要做的事情找到更好的解决方案!
希望这有帮助
I would advise you use a Service rather than an activity in this case.
A service runs in the background and is not affected by the activity lifecycle if it is started correctly.
One important thing to remember is that when you go back to the UI, the service must explicitly call the UI thread, otherwise you will get ANR errors, as the UI thread is not threadsafe.
Please read through that document, it should help you get a better solution for what you're trying to do!
Hope this helped
这是解决方案:
编辑:如果您想检查特定活动(类别),请使用此;
在你的清单文件中:
所以主要逻辑是打开和读取过程。
Here is the solution:
EDIT: If you want to check specific activity (class) use this;
And at your Manifest File:
So the main logic is opening and reading process.