BroadcastReceiver的本质属于一种监听器,因此要实现BroadcastReceiver,只需重写BroadcastReceiver的OnReceive(Context context,Intent intent)方法就可以.

设置可以接受广播:

调用registerReceiver(BroadcastReceiver receiver,IntentFilter filter)

IntentFilter filter = new IntentFilter("com.example.demo.MUSIC_RECEIVED");
MyReceiver serviceReceiver = new MyReceiver();
registerReceiver(serviceReceiver, filter);


在AndroidMannifest.xml文件中配置


<receiver android:name="com.example.demo.MyReceiver">
		<intent-filter >
			<action android:name="com.example.demo.MUSIC_RECEIVED" />
		</intent-filter>
</receiver>

发送广播:

sendBroadcast(Intent intent)
sendBroadcast(Intent intent,String receiverPermission)
sendOrderedBroadcast(Intent intent,String receiverPermission,BroadcastReceiver resultReceiver,Handler
scheduler,int initialCode,String initialData,Bundle initialExtras)
sendOrderedBroadcast(Intent intent,String receiverPermission)
sendStickyBroadcast(Intent intent)
sendStickyOrderedBroadcast(Intent intent,BroadcastReceiver resultReceiver,Handler scheduler,int initialCode,String initialData,Bundle initialExtras)