Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Broadcast Receiver and ResultReceiver in android

Tags:

android

What is difference between BroadcastReceiver and ResultReceiver in android?

like image 916
user1670564 Avatar asked Nov 01 '12 13:11

user1670564


People also ask

What is broadcast receivers in Android?

A broadcast receiver (receiver) is an Android component which allows you to register for system or application events. All registered receivers for an event are notified by the Android runtime once this event happens.

What are receiving and broadcasting intents in Android?

Broadcast intents are a mechanism by which an intent can be issued for consumption by multiple components on an Android system. Broadcasts are detected by registering a Broadcast Receiver which, in turn, is configured to listen for intents that match particular action strings.

What is difference between service and broadcast receiver in Android?

A Service receives intents that were sent specifically to your application, just like an Activity. A Broadcast Receiver receives intents that were broadcast system-wide to all apps installed on the device.

How many broadcast receivers are available in Android?

There are two types of broadcast receivers: Static receivers, which you register in the Android manifest file. Dynamic receivers, which you register using a context.


2 Answers

Result Receiver:

Generic interface for receiving a callback result from someone. 

Broadcast Receiver:

Base class for code that will receive intents sent by sendBroadcast(). 

EDIT:

Background: All networking operations/long running operations should take place away from the main thread. Two ways to do this :

  1. Async task - For Simple networking like say retreive an image/ do db processing
  2. Service - For Complex long running background process

If you need to perform work outside your main thread, but only while the user is interacting with your application, then you should probably instead create a new thread and not a service. For example, if you want to play some music, but only while your activity is running, you might create an Async Thread. But if you want the process to continue even after the user exits the app (say a download) then use a service

Lets say you pick 2. Now

  1. You activity sends a web request to your service

  2. Your service executes that using say DefaultHttpClient

  3. It sends back data to your activity.

    The third step of receiving data here can be done in two ways

1.) Broadcast receiver: Multiple receivers can receive your data. Used if you want to send data/notifications across applications(say you are also interacting with fb and twitter, multiple receivers for your web broadcast), whenever you send broadcast its sent system wide.

2.) Result receiver: Your application is the only receiver of the data. It is an Interface you implement and pass it to the intentService through putExtra. IntentService will then fetch this object and call its receiver.send function to send anything (in bundle) to calling activity. Result receiver has preference over broadcast receivers if your all communication is internal to your application

EDIT: I should also mention this caution

Caution: A service runs in the main thread of its hosting process—the service does not create its own thread and does not run in a separate process (unless you specify otherwise). This means that, if your service is going to do any CPU intensive work or blocking operations (such as MP3 playback or networking), you should create a new thread within the service to do that work. By using a separate thread, you will reduce the risk of Application Not Responding (ANR) errors and the application's main thread can remain dedicated to user interaction with your activities.

like image 102
Vrashabh Irde Avatar answered Oct 22 '22 08:10

Vrashabh Irde


A BroadcastReceiver is a receiver receiving broadcasts. Those are sent by someone in the intention that there can be many receivers receiving them (like radio broadcasts).

A ResultReceiver on the other hand is intended to receive a callback result from someone. So this could be compared with a walkie talkie, where you call someone and then are going to receive an answer (a result) from the one you called.

like image 25
Ridcully Avatar answered Oct 22 '22 07:10

Ridcully