Is it possible that BroadcastReceiver in an activity which have gone to background receiving broadcast? If not, what should I do?
EDIT:
Let me clarify what I really need.
I have a service that will receive data from bluetooth.
When it get data from blue-tooth device successfully, the service will broadcast.
And there are three cases of the activity.
- foreground
Popup a dialog and ask for user something
I think it can be simply done by register receiver in activity
- background
I just don’t know how to do this part.
If I register the receiver in application, notification is shown. After taking a look of the answers below, I think I can register in Application.
- sleep
Wake up the device.
Thank You.
You probably don’t even want that. Activities are not designed to be used that way.
You should use a service to handle it. Or start an Activity if it needs to have an UI.
There two alternatives the way I see it.
Please keep in mind that the receiver will run on the main thread, so you should signal to a running service or start a service or activity here. Perhaps a service that performs a single task.
A service that performs a single task can easily be implemented using IntentService. It is kind of like an async task wrapped in a service.