美文网首页Android开发经验谈Android技术知识Android开发
关于android8.0收不到自定义广播(静态广播)

关于android8.0收不到自定义广播(静态广播)

作者: Matsonga | 来源:发表于2019-04-15 22:29 被阅读16次

先看一下包名

看红线和黄线


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();

相关文章

网友评论

    本文标题:关于android8.0收不到自定义广播(静态广播)

    本文链接:https://www.haomeiwen.com/subject/bfrmwqtx.html