先看一下包名
![](https://img.haomeiwen.com/i3697372/b3341d98f2c46c0b.png)
AndroidManiFfest.xml
<receiver android:name=".MyReceiver"> // 和黄线一样
<action android:name="android.intent.action.MY_BROADCAST"/>
<category android:name="android.intent.category.DEFAULT" />
</receiver>
![](https://img.haomeiwen.com/i3697372/ff417f0c212f7adc.png)
MainActivity
Intent intent =new Intent("android.intent.action.MY_BROADCAST");
intent.putExtra("msg", "hello receiver.");
// 下面这句话很重要,8.0以后必须要加这段代码不然收不到广播!!!
intent.setComponent(new ComponentName("com.example.matsonga.broadcast_sms", //包名。 和红线一样
"com.example.matsonga.broadcast_sms.MyReceiver" //MyReceiver文件所在路径。和黄线一样));
sendBroadcast(intent);
![](https://img.haomeiwen.com/i3697372/dda14e99f1ed92c1.png)
MyReceive
String msg = intent.getStringExtra("msg");
Toast.makeText(context,"时间:"+new SimpleDateFormat("yyyy-MM-dd hh.mm.ss").format(new Date())
+"\nMyReceiver收到Action名为:"+intent.getAction().toString()
+"的广播 \nComponent:"+intent.getComponent()
+"\n传过来的消息:"+msg,
Toast.LENGTH_LONG).show();
![](https://img.haomeiwen.com/i3697372/ba3532f6450f5b41.png)
网友评论