//点击按钮发送一条无序广播 public void click(View v) { Intent intent = new Intent(); //设置action intent.setAction("com.itheima.custombroadcast"); intent.putExtra("name", "新闻联播每天晚上7点准时开整!!!"); //发送一条广播 发送无序广播 sendBroadcast(intent); }
import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.widget.Toast; public class ReceiveCustomReceiver extends BroadcastReceiver { //当我发送自定义广播的时候 这个方法就会接收到 @Override public void onReceive(Context context, Intent intent) { //[0]终止广播 // abortBroadcast(); //[1]取出我们发送广播携带的数据 String content = intent.getStringExtra("name"); //[2]把获取到的数据展示到toast上 Toast.makeText(context, content, 0).show(); } }
<receiver android:name="com.itheima.receivebroadcast.ReceiveCustomReceiver"> <intent-filter > <action android:name="com.itheima.custombroadcast"/> </intent-filter> </receiver>