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 */);
}
}
The
setResult(...)method inBroadcastReceiver, its more than anything for tracking purposes.If you are familiarised with the
Activity‘s methodsetResult(...), you can think of this method in broadcast in the same way. But instead of getting a callback method likeonActivityResult(int requestCode, int resultCode, Intent data)in the case of activities, broadcastsetResult(...)method is used to keep track of the results of the broadcasts in a certain order, that’s why the documentation says:Which means that you can make use of the methods
getResultCode(),getResultData()orgetResultExtras()to know how things went during the execution of theonReceive(Context, Intent)method in all the differentBroadcastReceivers registered to handle your broadcast. So you can know the result of the code execution in the previousBroadcastReceivercalled 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.