Created
March 2, 2014 22:10
-
-
Save csdear/c13e1f325c126bafa903 to your computer and use it in GitHub Desktop.
Multiple Broadcast Receivers : Ordered Priority
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
/* 3 Broadcast Receivers | |
BR1 : Priority 3 - Dynamic BR. Also has a abort method which consumes the intent. | |
BR2 : Priority 10 - Statically Declared | |
BR3 : Priority 1 -Statically Declared | |
AndroidManifest.xml | |
*/ | |
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="course.examples.BroadcastReceiver.CompoundOrderedBroadcast" | |
android:versionCode="1" | |
android:versionName="1.0" > | |
<uses-sdk | |
android:minSdkVersion="10" | |
android:targetSdkVersion="18" /> | |
<uses-permission android:name="android.permission.VIBRATE" > | |
</uses-permission> | |
<application | |
android:allowBackup="false" | |
android:icon="@drawable/icon" | |
android:label="@string/app_name" > | |
<activity | |
android:name=".CompoundOrderedBroadcast" | |
android:label="@string/app_name" > | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
//1. Set priority BR2, 10 | |
<receiver | |
android:name=".Receiver2" | |
android:exported="false" > | |
<intent-filter android:priority="10" > | |
<action android:name="course.examples.BroadcastReceiver.show_toast" > | |
</action> | |
</intent-filter> | |
</receiver> | |
//2. Set priority BR3, 1 | |
<receiver | |
android:name=".Receiver3" | |
android:exported="false" > | |
<intent-filter android:priority="1" > | |
<action android:name="course.examples.BroadcastReceiver.show_toast" > | |
</action> | |
</intent-filter> | |
</receiver> | |
</application> | |
</manifest> | |
======================================= | |
//A compound ordered broadcast | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
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"; | |
//3. BR3, dynamic created receiver | |
private final Receiver1 mReceiver = new Receiver1(); | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.main); | |
//4. Set priority | |
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 | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
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"); | |
// abortBroadcast -- this BR consumes the intent an aborts the broadcast if it is ordered. | |
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 | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
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(); | |
} | |
} | |
================================================================ | |
// BR3 | |
package course.examples.BroadcastReceiver.CompoundOrderedBroadcast; | |
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(); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment