booksource
booksource copied to clipboard
第一行代码 Android 179页 5.3章节 发送自定义广播代码遗漏
在学习到第5.3章节 5.3.1 发送标准广播时发现书中代码只提供了发送标准广播的代码,没有提供监听代码,书中给出了...省略号,误以为不需要监听就可以了,调试了许久看了这里的源码才知道。
不是有代码遗漏,是 Android 8.0 之后,对于广播的发送与接收变严格了,需要加入Component参数。
Intent intent = new Intent("com.example.broad");
intent.setComponent(new ComponentName("com.example.broadcasttest","com.example.broadcasttest.MyBroadcastReceiver"));
sendBroadcast(intent);