Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why call setResult in BroadcastReceiver?

in the C2DM sample code from google, when a notification recived in BroadcastReceiver they call :

setResult(Activity.RESULT_OK, null /* data */, null /* extra */);

I didnt know what the setResult do. this is what they say in Android docs :

Change all of the result data returned from this broadcasts; only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method.

Can somebody explain what they mean and why i need to call it?

Complete code :

public class C2DMBaseReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        C2DMReceiver.runIntentInService();
        setResult(Activity.RESULT_OK, null /* data */, null /* extra */);
    }
}
like image 226
Jimmy Avatar asked Jan 24 '12 16:01

Jimmy


People also ask

What is the method name in BroadcastReceiver receive the message?

Receive SMS messages with a broadcast receiver. To receive SMS messages, use the onReceive() method of the BroadcastReceiver class. The Android framework sends out system broadcasts of events such as receiving an SMS message, containing intents that are meant to be received using a BroadcastReceiver.

What is the use of BroadcastReceiver in Android?

Android BroadcastReceiver is a dormant component of android that listens to system-wide broadcast events or intents. When any of these events occur it brings the application into action by either creating a status bar notification or performing a task.

What is the role of the onReceive () method in the BroadcastReceiver?

The implementing class for a receiver extends the BroadcastReceiver class. If the event for which the broadcast receiver has registered happens, the onReceive() method of the receiver is called by the Android system.

When would you use a BroadcastReceiver?

Broadcast in android is the system-wide events that can occur when the device starts, when a message is received on the device or when incoming calls are received, or when a device goes to airplane mode, etc. Broadcast Receivers are used to respond to these system-wide events.


1 Answers

The setResult(...) method in BroadcastReceiver, its more than anything for tracking purposes.

If you are familiarised with the Activity's method setResult(...), you can think of this method in broadcast in the same way. But instead of getting a callback method like onActivityResult(int requestCode, int resultCode, Intent data) in the case of activities, broadcast setResult(...) method is used to keep track of the results of the broadcasts in a certain order, that's why the documentation says:

Only works with broadcasts sent through Context.sendOrderedBroadcast. All current result data is replaced by the value given to this method.

Which means that you can make use of the methods getResultCode(), getResultData() or getResultExtras() to know how things went during the execution of the onReceive(Context, Intent) method in all the different BroadcastReceivers registered to handle your broadcast. So you can know the result of the code execution in the previous BroadcastReceiver called before the one currently executed along all the receivers.

It says only Context.sendOrderedBroadcast() because a regular call to sendBroadcast(...) method might not wait for 1 receiver to complete its execution before starting another thread to execute code in other receiver listening the same intent as well.

like image 55
Mago De OZ Avatar answered Sep 20 '22 15:09

Mago De OZ