Created
March 2, 2014 23:27
-
-
Save csdear/5d769189f1646bb600ae to your computer and use it in GitHub Desktop.
Multi-Ordered Broadcast Receivers with ResultReceiver
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Good for tallying up results from multiple intents at the end. | |
//Defines the ResultReceiver in sendOrderedBroadcast | |
// Thus finally receives result after all other BRs have modified. | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
import android.app.Activity; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.content.IntentFilter; | |
import android.os.Bundle; | |
import android.view.View; | |
import android.view.View.OnClickListener; | |
import android.widget.Button; | |
import android.widget.Toast; | |
public class CompoundOrderedBroadcastWithResultReceiver extends Activity { | |
static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.show_toast"; | |
private final Receiver1 mReceiver1 = new Receiver1(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
IntentFilter intentFilter = new IntentFilter(CUSTOM_INTENT); | |
intentFilter.setPriority(3); | |
registerReceiver(mReceiver1, intentFilter); | |
Button button = (Button) findViewById(R.id.button); | |
button.setOnClickListener(new OnClickListener() { | |
@Override | |
public void onClick(View v) { | |
//1. This receiver will receive the last intent after all other BRs have went before him | |
//Useful for computing a result at the end using getResultData() | |
sendOrderedBroadcast(new Intent(CUSTOM_INTENT), null, | |
new BroadcastReceiver() { | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Toast.makeText(context, | |
"Final Result is " + getResultData(), | |
Toast.LENGTH_LONG).show(); | |
} | |
}, null, 0, null, null); | |
} | |
}); | |
} | |
@Override | |
protected void onDestroy() { | |
unregisterReceiver(mReceiver1); | |
super.onDestroy(); | |
} | |
} | |
=================================================== | |
//Receiver1 | |
//Final action is a message showing which receivers received the intent and the | |
//order in which they received it. | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
public class Receiver1 extends BroadcastReceiver { | |
private final String TAG = "Receiver1"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.i(TAG, "INTENT RECEIVED by Receiver1"); | |
//2. BR1 getsResultData then adds to it, a new string by calling setResultData. | |
String tmp = getResultData() == null ? "" : getResultData(); | |
setResultData(tmp + ":Receiver 1:"); | |
} | |
} | |
================== | |
//Receiver2 | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
public class Receiver2 extends BroadcastReceiver { | |
private final String TAG = "Receiver2"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.i(TAG, "INTENT RECEIVED by Receiver2"); | |
String tmp = getResultData() == null ? "" : getResultData(); | |
setResultData(tmp + "Receiver 2:"); | |
} | |
} | |
================ | |
//Receiver3 | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
import android.content.BroadcastReceiver; | |
import android.content.Context; | |
import android.content.Intent; | |
import android.util.Log; | |
public class Receiver3 extends BroadcastReceiver { | |
private final String TAG = "Receiver3"; | |
@Override | |
public void onReceive(Context context, Intent intent) { | |
Log.i(TAG, "INTENT RECEIVED by Receiver3"); | |
String tmp = getResultData() == null ? "" : getResultData(); | |
setResultData(tmp + ":Receiver 3"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment