BroadcastReceiver之发送自定义无序广播

首先,发送一个自定义广播:(用sendBroadcast(intent)发送一条无序广播)

1 public void  click(View v){
2         Intent intent = new Intent();
3         intent.putExtra("msg","我是一条无序广播");
4         intent.setAction("com.coderwei.myBroadcast");
5         sendBroadcast(intent);
6     }

然后自定义一个类继承BroadcastReceiver接收广播,先配置Manifest.xml

<receiver android:name=".ReceiverBroadcast">
            <intent-filter>
                <action android:name="com.coderwei.myBroadcast"/>
            intent-filter>
 receiver>

接收这个广播,输出携带的数据

1 public class ReceiverBroadcast extends BroadcastReceiver {
2     @Override
3     public void onReceive(Context context, Intent intent) {
4         String str = intent.getStringExtra("msg");
5         System.out.println(str);
6     }
7 }

 

ps:上面的action需要自定义的

转载于:https://www.cnblogs.com/Godfunc/p/6022542.html

你可能感兴趣的:(BroadcastReceiver之发送自定义无序广播)