Android中BroadCast主要有普通广播和有序广播
普通广播的发送方式为sendBroadcast有序广播的发送方式为sendOrderedBroadcast
- 另外还需要知道本地广播(broadcastManager.sendBroadcast)
普通广播(sendBroadcast)
普通广播即标准广播,可在manifest文件中注册或者在java代码中动态注册(官方建议动态注册时在onResume()中注册广播,在onPause()中取消注册),普通广播发送后其所有接收方会同时(逻辑上)接收到广播,可跨进程通信。
1.编写Broadcast类
package com.space.broadcastreceiver.ordinary; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by space on 2016/8/28. */ public class OrdinaryBroadCastReceiver extends BroadcastReceiver{ private final static String TAG = "OrdinaryBroadCastReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i("space", TAG + "------onReceive 接收到广播 参数 "+intent.getStringExtra("from")+" "+intent.getStringExtra("to")); } }
2.注册广播
方式一:在manifest中注册
<receiver android:name=".ordinary.OrdinaryBroadCastReceiver"> <intent-filter> <action android:name="com.space.OrdinaryBroadCastReceiver"></action> </intent-filter> </receiver>
方式二:在Activity中注册/取消
//注册广播(一般写在onResume()中) receiver = new LocalBroadCastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction("com.space.OrdinaryBroadCastReceiver"); registerReceiver(receiver, intentFilter); //取消注册广播(一般写在onPause()中) unregisterReceiver(receiver);
3.发送普通广播
intent = new Intent("com.space.OrdinaryBroadCastReceiver"); intent.putExtra("from","MainActivity"); intent.putExtra("to", "OrdinaryBCReceiver"); sendBroadcast(intent);
有序广播(sendOrderedBroadcast)
有序广播,在manifest文件中注册或者在java代码中动态注册(官方建议动态注册时在onResume()中注册广播,在onPause()中取消注册),有序广播发送后其所有接收者按照优先级进行接收广播,优先级高的接受者有权中断广播,也可将处理结果传递给之后的接受者。
1.编写Broadcast类(这里写了四个广播接收的类,按照优先级进行广播接收)第一个广播接收类
package com.space.broadcastreceiver.order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; /** * Created by space on 2016/8/28. */ public class OrderReceiverOne extends BroadcastReceiver { private final static String TAG = "OrderReceiverOne"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); int i = -1; if(bundle != null){ i = bundle.getInt("count"); bundle.putInt("count",i+1); bundle.putInt("countOne",-200); } Log.i("space", TAG + "------onReceive 接收到广播 参数"+i); setResultExtras(bundle); } }
第二个广播接收类
package com.space.broadcastreceiver.order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; /** * Created by space on 2016/8/28. */ public class OrderReceiverTwo extends BroadcastReceiver { private final static String TAG = "OrderReceiverTwo"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); int i = -1; int countOne = -1; if(bundle != null){ i = bundle.getInt("count"); countOne = bundle.getInt("countOne"); bundle.putInt("count", i+1); bundle.putInt("countTwo", -300); } Log.i("space", TAG + "------onReceive 接收到广播 参数" + i + " "+countOne); setResultExtras(bundle); } }
第三个广播接收类
package com.space.broadcastreceiver.order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; /** * Created by space on 2016/8/28. */ public class OrderReceiverThree extends BroadcastReceiver { private final static String TAG = "OrderReceiverThree"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = getResultExtras(true); int i = -1; int countOne = -1; int countTwo = -1; if(bundle != null){ i = bundle.getInt("count"); countOne = bundle.getInt("countOne"); countTwo = bundle.getInt("countTwo"); bundle.putInt("count",i+1); bundle.putInt("countThree",-400); } Log.i("space", TAG + "------onReceive 接收到广播 参数"+i+" "+countOne+" "+countTwo+" 截断广播"); setResultExtras(bundle); abortBroadcast(); } }
第四个广播接收类
package com.space.broadcastreceiver.order; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.util.Log; /** * Created by space on 2016/8/28. */ public class OrderReceiverFour extends BroadcastReceiver { private final static String TAG = "OrderReceiverFour"; @Override public void onReceive(Context context, Intent intent) { Bundle bundle = intent.getExtras(); int i = -1; if(bundle != null){ i = bundle.getInt("count"); bundle.putInt("count",i+1); } Log.i("space", TAG + "------onReceive 接收到广播 参数" + i + " " +getResultData()); } }
2.注册广播
<receiver android:name=".order.OrderReceiverOne"> <intent-filter android:priority="100"> <action android:name="com.space.OrderReceiver"></action> </intent-filter> </receiver> <receiver android:name=".order.OrderReceiverTwo"> <intent-filter android:priority="99"> <action android:name="com.space.OrderReceiver"></action> </intent-filter> </receiver> <receiver android:name=".order.OrderReceiverThree"> <intent-filter android:priority="98"> <action android:name="com.space.OrderReceiver"></action> </intent-filter> </receiver> <receiver android:name=".order.OrderReceiverFour"> <intent-filter android:priority="97"> <action android:name="com.space.OrderReceiver"></action> </intent-filter> </receiver>
3.发送有序广播
intent = new Intent("com.space.OrderReceiver"); intent.putExtra("count", 0); sendOrderedBroadcast(intent, null);
4.结果
从截图可以看到广播确实是按照在manifest中设置的优先级接收到广播的,并且优先级高的可以操作广播中传输的数据,并且可以截断广播(接收器四没有收到广播)
本地广播(broadcastManager.sendBroadcast)
本地广播,在manifest文件中注册或者在java代码中动态注册(官方建议动态注册时在onResume()中注册广播,在onPause()中取消注册),本地广播的特点是发送后只有当前app能够接收到广播,不用担心其他应用接收到广播,避免数据泄露。
1.编写Broadcast类
package com.space.broadcastreceiver.local; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.util.Log; /** * Created by space on 2016/8/28. */ public class LocalBroadCastReceiver extends BroadcastReceiver{ private final static String TAG = "LocalBroadCastReceiver"; @Override public void onReceive(Context context, Intent intent) { Log.i("space", TAG + "------onReceive 接收到广播"); } }
2.注册/取消 广播
private LocalBroadcastManager broadcastManager; private BroadcastReceiver receiver; private final String mLocalAction = "com.space.localReceiver"; //注册广播(一般写在onResume()中) receiver = new LocalBroadCastReceiver(); IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(mLocalAction); broadcastManager.registerReceiver(receiver, intentFilter); //取消注册广播(一般写在onPause()中) broadcastManager.unregisterReceiver(receiver);
3.发送广播
Intent intent = new Intent(mLocalAction); broadcastManager.sendBroadcast(intent);
至此已经介绍了普通广播与有序广播的使用
另外如果有不明白的地方可以留言。有写错或者不好的地方欢迎指正