先看一下包名
AndroidManiFfest.xml
<receiver android:name=".MyReceiver"> // 和黄线一样
<action android:name="android.intent.action.MY_BROADCAST"/>
<category android:name="android.intent.category.DEFAULT" />
</receiver>
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);
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();