Reputation: 4520
Beginner in android, so please bear with me...
According to the docs, you should never start an activity from a broadcast receiver:
"(...)Never start an Activity in response to an incoming broadcast Intent."
http://developer.android.com/training/run-background-service/report-status.html
(last line of the docs).
So, here's my question: since I need to show the activity chooser from a background service and since I should never start an activity from a broadcast receiver, then how can I solve my problem?
Upvotes: 2
Views: 594
Reputation: 29722
Your exact situation needs more clarification. Exactly what are you trying to do?
In the meantime, here is the background explanation that should help you to understand when this rule applies, and if you should break it in your app.
"(...)Never start an Activity in response to an incoming broadcast Intent."
This has to do with the idea of giving Notification
to the user by using the notification bar. The idea is that you should never interrupt the user from what they are trying to do at the time.
Android appreciates that the user knows what they want to do at any time, and the OS has no right to interrupt the user. This is in direct contrast to other systems that use popups to alert the user of incoming event.
So the idea behind your quote, is that you should never pull a user's attention out of his current activity, by launching your own activity over it.
This could be terrible, imagine you are in the middle of a game, and suddenly Luis's activity shows in front of the game.
However, there are exceptions to the rule:
If the user is already using your app, and you receive the Intent
, then your BroadcastReceiver
should launch your own activity.
When the user is not using your app, then post the Notification
instead.
You can see this type of behaviour in apps like Whatsapp. If you are in the app, you don't get the notification, instead the app just shows the new message. If you are out of the app, you get the notification.
Sometimes starting an activity like this is expected behaviour, it makes sense.
One obvious example is a phone app. It must launch when the phone rings, and we have come to accept that.
Here is another legitimate use-case of starting an activity from a receiver. It is a clock app that is designed to launch when the phone is put on charge:
Upvotes: 2