Robotic Vn
Robotic Vn

Reputation: 577

Difference between intent-filter in activity and broadcastreceiver

can anybody tell me the difference between intent-filter in activity and broadcastreceiver? Thank you very much!

<activity>
          <intent-filter></intent-filter>
</activity>

and

<receiver>
      <intent-filter></intent-filter>
 </receiver>

Upvotes: 0

Views: 828

Answers (2)

Priya B
Priya B

Reputation: 96

When an app starts an activity using an intent, it only starts one activity (possibly showing the "Complete action using..." dialog to let you choose which app you want to open it with), and the same goes for services, but broadcasting an intent may start several broadcast receivers, possibly from different apps.

You can get a more detailed idea here.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006934

I register a BroadcastReceiver to listen event of SMS arrives. Now, I want to use intent-filter in activity. Can I do it?

You can certainly have an <intent-filter> for an <activity>. Some do, such as your launcher activity.

However, if you try using the same <intent-filter> on an <activity> that you are using on a <receiver> to "listen event of SMS arrives", that will not work. The reason it will not work has nothing to do with the <intent-filter>, but rather with the Intent.

For inter-process communication (IPC) in Android, we usually use an Intent for one of three things:

  • starting an activity
  • starting or binding to a service
  • sending a broadcast

These are completely independent. You can think of them as three separate channels on a TV, or three separate train tracks.

When an SMS arrives, the system will send out one (or more) broadcasts related to that event. Since they are broadcasts, you can listen for them with a <receiver>. Since they are broadcasts, you cannot listen for them using an <activity> or a <service>.

You are welcome to have a <receiver> call startActivity() to start up an activity, which is almost like having an <activity> directly respond to the broadcast. However, while this is technically possible, it is rarely the right answer, as users usually will not appreciate being interrupted in whatever they are doing by your activity popping up without warning.

Upvotes: 2

Related Questions