Knowing if activity is running on foreground or background - android

I use certain sounds to notify the user of certain events. However, I do not want the app to notify the user with sounds if the activity is not on foreground.
How can I check if the app is running on background or not?

Expanding on mco's answer, your GCMIntentService needs to do something that will trigger work in a foreground activity of yours, if you have a foreground activity. Typically, you will do this by setting up your activities to respond to some sort of message in onResume() and removing that in onPause().
"Some sort of message" could be:
An Intent sent via LocalBroadcastManager
An Intent sent via the classic sendBroadcast()
A message via a message bus, like Otto
Etc.

In the Activity cycle, onResume is called when the app becomes visible (foreground) and onPause is called when it is not visible (background).
You can use these functions to do whatever you want to do when the app is background/foreground.

Related

Event on launch/stop of visible part of application

Stackoveflow!
I have an application with sticky background service and visible part, consisted of several activities.
I need to track start and stop of visible part (all activities).
For example:
When user starts MainActivity, Service receives LocalBroadcast that tells it to start something.
When user rotates screen, Service must not receive anything.
When user goes to SecondActivity, Service again must not receive anything.
When user closes ALL ACTIVITIES, Service gets LocalBroadcast telling it to stop something.
If I use Activity. onCreate and onDestroy, or onStart and onStop, I get events related to lifecycle of single activity. Also I get events related to screen rotation.
I also cannot use Application. onCreate or onTerminate, as they will not trigger because of service running in the background.
I need to track real start and stop of entire application except service.
See: http://developer.android.com/guide/components/bound-services.html
When the last client unbinds from the service, the system destroys
the service (unless the service was also started by startService()).
So each Activity should bind() to the service, in onCreate, and unbind() in onStop/onDestroy
Now to fix the problem with the orientation changes, you can check for isFinishing in the onStop/onDestory callback
(see: How to distinguish between orientation change and leaving application android)
and also put some flag in onSaveInstanceState callback, so that in the following onCreate you can check for the existence of that flag, and act accordingly (refresh the binding, or avoid calling bind() again)

Responding to broadcast message from activity

I have an intent service that broadcasts a message based on user action and my activity is registered to receive the message.
Now, after the activity receives the message, I want my activity to respond to it depending on the message passed.
I have the below questions:
1. Will the receiver be still called even if my activity is killed? I don't want this to happen but would like to know if this is possible
2. Should I check for the current state of the activity (like which life-cycle method is currently being executed) before updating the activity UI in response to the message? Are there any best practices for handling the same?
Any helpful insight is appreciated. Thanks in advance
The Receiver should be a member of the Activity.
Register # onResume(), unRegister # onPause(), or onStart()&onStop()
Now to answer your questions:
No, if u unregister it at the correct place in the lifecycle there is no reason
that the receiver will be called.
No, because if its called then u know that the activity is visible to the user. But u need to check the UI elements because their state could change.

How can my service be notified when the Home or Back buttons are pressed

I would like to have my service perform something when the user presses Home or Back.
I'm not looking to override or block the normal behavior of these buttons, just get notified about them.
I need to get notify even when the activity that started the service is stopped.
Working with platform 17-19.
Is there any way?
Can it be done by intent-filter or broadcast receivers??
Thanks
Only Activities (and Fragments) will get these callbacks (onPause, onStop etc.). If your Activity is running, it can notify the service, but once it's in the background there is no way to do this.
If the service is on you can use a broadcast by hooking into onPause or on onStop methods in the activity. if the service is not on I think you have to start it, do something, then end.

Why the receiver defined in manifest is not receiving broadcasts when used with LocalBroadcastManager?

Few days ago I read that there is a better mechanism for sending broadcasts within single application - the LocalBroadcastManager.
It works well (just like standard broadcasts..). However, yesterday I've found out that it cannot send broadcasts to receivers, which are defined in the manifest (when I temporarily switched it to use the standard Activity's sendBroadcast method, it worked).
The reason why I want this (and correct me if there is a more preferred way to do it) is:
Lets's say I want to download a file. I will use a service, because that's how Android wants us to do. OK, now I want to display (and periodically update) its progress in my activity. So service will be sending broadcasts to my activity and the activity has to register to receive them. The preferred way to handle broadcasts is to register in onResume() and unregister in onPause(). Now let's imagine that the user is bored with the slowly moving progressbar, so he presses Home and goes to do something else. Later he comes back and wants to see the current status of the download, but how can I tell him, when I unregistered from broadcasts that second he left my application?
That's why I use a receiver defined in the manifest, to be always ready to receive the broadcast and store it permanently (shared preferences, database...), so the activity can reconstruct the latest broadcast when it becomes visible.
However now I'm not sure, whether this routine is not recommended, or why the LocalBroadcastManager is not allowing me to do it.
If you are using SharedPreferences a workaround would be to make your activity implement OnSharedPreferenceChangeListener. So the service writes the pref and the activity listens for the change and updates progress bar. onResume() you also check the preference and update the UI accordingly.
The nice thing with this is you don't really have a leak if you fail to unregister them - see
Android : Where should a OnSharedPreferenceChangeListener be defined/registered - I prefer to unregister to onDestroy() as I want to have my activity updated even if not in the foreground - and the listener will go away even if onDestroy is not called.
As for why it does not work with manifest registered receivers - could you post some code ? Do you actually register the receivers with LBM ?

How can you tell if any Activity in your application is foregrounded?

I've got an Android app which has a periodic background Service. I want this Service to react differently depending on whether any Activities in my application are open. The issue is that the Service just keeps itself running via the AlarmManager making it on kind of on a separate "track" from the Activities, so I don't know whether the application is open when it runs.
The best solution I can think of is to flip a boolean on/off whenever onResume() or onPause() is called in all my Activities, but this seems like a lot of footwork. Is there a simpler, more elegant solution?
Is there a simpler, more elegant
solution?
Well, it depends a bit on what "react differently" means.
Let's suppose you want to raise a Notification if your activities are not in the foreground, but you want to pop a dialog if an activity is in the foreground.
In that case, you need some communication path from the service to the activity anyway. So, register a callback (supplied by the activity) with the service in onResume() and unregister it in onPause(). Your service uses the callback if one exists; if there is no callback, it raises the Notification.
You could accomplish the same thing with a broadcast Intent (register/unregister the receiver in the activity in onResume()/onPause()) if you wanted.

Categories

Resources