只显示如果活动没有显示通知 [英] Show a notification only if Activity is not showing
问题描述
我有一个后台任务,我想处理。问题是,在任务完成时,我想调用一个新的活动显示结果给用户,只有当我的主要活动是展示,否则我想送只是一个通知,使用户可以看到的动作完成,并能够每当他喜欢将其打开。
I have a background task I would like to handle. The thing is that when the task completes, I would like to call a new Activity to show the result to the user, only if my main Activity is showing, otherwise I would like to send just a notification so the user can see that the action completed, and be able to open it whenever he likes.
我在考虑使用一个服务来处理后台任务的开始和终止,并当它完成广播消息的,但在这种情况下,我别无选择知道活动是否被示出或广播没有处理和我应该发送通知。
I was thinking of using a service to handle the start and termination of the background task and broadcast a message when it finishes, but in this case I have no option to know whether the Activity was shown or the broadcast was not processed and I should send a notification.
因此,这是我的问题,因为我的知识和后台任务和服务的经验是有限的,我决定寻求帮助。
So this is my problem, and because my knowledge and experience in background tasks and services is limited I decided to ask for some help.
在此先感谢您阅读我的情况下,希望有一定的帮助!
Thanks in advance for reading my case, hope for some help!
推荐答案
下面是一个很好的文章,描述了如何实现你想要的东西:的Activity或通过有序广播的通知。
Here's a good article which describes how to implement what you want: Activity or Notification via Ordered Broadcast.
其主要思想是利用有序的广播。您应该创建一个的BroadcastReceiver
将生活在没有任何活动。为了做到这一点,你应该在的AndroidManifest.xml
文件声明。该接收器将显示一个通知
。你也应该注册另一个的BroadcastReceiver
与您的主要活动更高的优先级,这将显示屏幕上的内容。然后你只需要发送一个有序的广播。
The main idea is to use ordered broadcasts. You should create a BroadcastReceiver
which will live without any activities. In order to do that you should declare it in the AndroidManifest.xml
file. This receiver will show a Notification
. Also you should register another BroadcastReceiver
with higher priority in your main activity which will display something on the screen. Then you just need to send an ordered broadcast.
这篇关于只显示如果活动没有显示通知的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!