Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 358
akalipetis Avatar asked Aug 25 '11 13:08

akalipetis


1 Answers

Here's a good article which describes how to implement what you want: Activity or Notification via Ordered Broadcast.

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.

like image 162
Michael Avatar answered Nov 15 '22 13:11

Michael