user1166635
user1166635

Reputation: 2801

The differences between broadcast receiver and activity

I'm reading the documentation now, and I have 1 thing to be fixed - please, tell me, what is difference between broadcast receiver and activity (without the fact that activity can show UI)? Broadcast receiver gets announcements using intent-filter, but Activity can do it too! Please, make me clear. Thank you.

Upvotes: 3

Views: 2727

Answers (3)

Andreas
Andreas

Reputation: 2047

Activity is only active when you open it. When it is moved to the background, it is either paused or shut down.

A listener is always active on the background. The only thing that can "activate" a listener, is the thing it is listening for. Ex.: a broadcastlistener will detect (and react) when you receive a phonecall/sms, but will ignore the fact that you set your alarm (since it only pays attention to incoming/outgoing broadcasts)

the intent filter does pretty much the same thing for both, the difference is just how it is called. With activities, it requires the user to do something; with listeners, it requires the listener to be triggered.

Upvotes: 0

Kristopher Micinski
Kristopher Micinski

Reputation: 7672

You basically have it. An Activity is intended to be associated with a UI. By contrast a Broadcast receiver only 'catches' intents broadcast through the app / system. However, there are also implications for how the object is loaded into the system, and how long it sticks around. From the BroadcastReciever documentation:

A BroadcastReceiver object is only valid for the duration of the call to onReceive(Context, Intent). Once your code returns from this function, the system considers the object to be finished and no longer active.

This has important repercussions to what you can do in an onReceive(Context, Intent) implementation: anything that requires asynchronous operation is not available, because you will need to return from the function to handle the asynchronous operation, but at that point the BroadcastReceiver is no longer active and thus the system is free to kill its process before the asynchronous operation completes.

Keeping these differences in mind, the system may be able to more efficiently execute pieces of your app...

Upvotes: 1

vipin
vipin

Reputation: 3001

Activity is something which work on your input or require an user intruption for launching any task but with the help of broadcast reciever you can listen the system services as once a broadcast receiver is started for listening incoming calls then each time when a incoming call it will launch your method what you have written for that for more explanation check these

http://developer.android.com/reference/android/content/BroadcastReceiver.html

http://www.vogella.de/articles/AndroidServices/article.html

Upvotes: 2

Related Questions