CompoundOrderedBroadcast.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
import android.app.Activity; 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; public class CompoundOrderedBroadcast extends Activity { static final String CUSTOM_INTENT = "course.examples.BroadcastReceiver.show_toast"; private final Receiver1 mReceiver = 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(mReceiver, intentFilter); Button button = (Button) findViewById(R.id.button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { sendOrderedBroadcast(new Intent(CUSTOM_INTENT), android.Manifest.permission.VIBRATE); } }); } @Override protected void onDestroy() { unregisterReceiver(mReceiver); super.onDestroy(); } } |
Receiver1.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; import android.util.Log; import android.widget.Toast; public class Receiver1 extends BroadcastReceiver { private final String TAG = "Receiver1"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "INTENT RECEIVED"); if (isOrderedBroadcast()) { Log.i(TAG, "Calling abortBroadcast()"); abortBroadcast(); } Vibrator v = (Vibrator) context .getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(500); Toast.makeText(context, "INTENT RECEIVED by Receiver1", Toast.LENGTH_LONG).show(); } } |
Receiver2.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; import android.util.Log; import android.widget.Toast; public class Receiver2 extends BroadcastReceiver { private final String TAG = "Receiver2"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "INTENT RECEIVED"); Vibrator v = (Vibrator) context .getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(500); Toast.makeText(context, "INTENT RECEIVED by Receiver2", Toast.LENGTH_LONG).show(); } } |
Receiver3.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Vibrator; import android.util.Log; import android.widget.Toast; public class Receiver3 extends BroadcastReceiver { private final String TAG = "Receiver3"; @Override public void onReceive(Context context, Intent intent) { Log.i(TAG, "INTENT RECEIVED"); Vibrator v = (Vibrator) context .getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(500); Toast.makeText(context, "INTENT RECEIVED by Receiver3", Toast.LENGTH_LONG).show(); } } |
main.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="150dip" android:text="@string/broadcast_intent_string" android:textSize="24sp"> </Button> </LinearLayout> |