一、背景
在做项目过程中,在应用发送自定义广播,看到日志报错“Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx”,但是不会影响广播的发送和接收,查看资料为了广播安全,提醒 系统应用开发者去将 broadcast添加为protected-broadcast,因为非 protected-broadcast 广播是可以被三方应用发送的。 而定义为 proected-broadcast 就能防止恶意的三方应用模仿系统应用去发出该广播。
参考链接:https://blog.csdn.net/xiaowang_lj/article/details/128828286
修改位置:
/home/ss/X9-work/buildsystem/android/frameworks/base/core/res/AndroidManifest.xml
<!--lili add start 230530-->
<protected-broadcast android:name="com.xxx.music.speech_recognition_broadcast"/
<!--lili add end 230530-->
修改,整体编译之后,将程序烧录到设备之后,发现广播发送之后,接收者接收不到,报错:
W/BroadcastQueue( 3049): Background execution not allowed: receiving Intent { act=com.xxx.music.speech_recognition_broadcast flg=0x10 (has extras) } to com.htkj.music/.broadcast.SpeechRecognitionBroadcast
查找解决方案,在发送广播时,添加接收应用包名:
public void commonSendBroadcast(Context context, String pkgName, String action, String extra, int type) {
Intent intent = new Intent();
intent.setPackage(pkgName);
intent.setAction(action);
if (extra != null && !"".equals(extra)) {
intent.putExtra(extra, type);
}
context.sendBroadcast(intent);
}
解决!!