当前位置:   article > 正文

Android发送广播时报错:Sending non-protected broadcast xxxxxxx from system xxxxxxxxxx

sending non-protected broadcast

带android:sharedUserId=“android.uid.system” 发送广播时,会出现 Sending non-protected broadcast 异常提醒;

原因:
Ams在发送广播时,对于systemApp(系统应用),会要求发送广播必须是声明在frameworks\base\core\res\AndroidManifest.xml里面的protected-broadcast。这是为了提醒 系统应用开发者要将 broadcast 添加到protected-broadcast,因为非 protexted-broadcast 广播是可以被三方应用发送的。定义为 proected-broadcast 可以避免三方垃圾应用也发送这些广播来捣蛋。

查找问题

首先去搜索问题出现的位置 ,去AndroidXRef网站搜索下

定位到frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

类中,在checkBroadcastFromSystem中抛出该异常。

checkBroadcastFromSytem是用于检测系统应用发出的广播是否安全,如果发出的广播不安全,就会抛出no-protected broadcast异常,提醒系统应用开发者。

为什么要有这个机制?

这是为了提醒 系统应用开发者去将 broadcast添加为protected-broadcast,因为非 protected-broadcast 广播是可以被三方应用发送的。 而定义为 proected-broadcast 就能防止恶意的三方应用模仿系统应用去发出该广播。

  1. private void checkBroadcastFromSystem(Intent intent, ProcessRecord callerApp,
  2. String callerPackage, int callingUid, boolean isProtectedBroadcast, List receivers) {
  3. if ((intent.getFlags() & Intent.FLAG_RECEIVER_FROM_SHELL) != 0) {
  4. // Don't yell about broadcasts sent via shell
  5. return;
  6. }
  7. final String action = intent.getAction();
  8. if (isProtectedBroadcast
  9. || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
  10. || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
  11. || Intent.ACTION_MEDIA_BUTTON.equals(action)
  12. || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
  13. || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
  14. || Intent.ACTION_MASTER_CLEAR.equals(action)
  15. || Intent.ACTION_FACTORY_RESET.equals(action)
  16. || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
  17. || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
  18. || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
  19. || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
  20. || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)
  21. || AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION.equals(action)
  22. || AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION.equals(action)) {
  23. // Broadcast is either protected, or it's a public action that
  24. // we've relaxed, so it's fine for system internals to send.
  25. return;
  26. }
  27. // This broadcast may be a problem... but there are often system components that
  28. // want to send an internal broadcast to themselves, which is annoying to have to
  29. // explicitly list each action as a protected broadcast, so we will check for that
  30. // one safe case and allow it: an explicit broadcast, only being received by something
  31. // that has protected itself.
  32. if (intent.getPackage() != null || intent.getComponent() != null) {
  33. if (receivers == null || receivers.size() == 0) {
  34. // Intent is explicit and there's no receivers.
  35. // This happens, e.g. , when a system component sends a broadcast to
  36. // its own runtime receiver, and there's no manifest receivers for it,
  37. // because this method is called twice for each broadcast,
  38. // for runtime receivers and manifest receivers and the later check would find
  39. // no receivers.
  40. return;
  41. }
  42. boolean allProtected = true;
  43. for (int i = receivers.size()-1; i >= 0; i--) {
  44. Object target = receivers.get(i);
  45. if (target instanceof ResolveInfo) {
  46. ResolveInfo ri = (ResolveInfo)target;
  47. if (ri.activityInfo.exported && ri.activityInfo.permission == null) {
  48. allProtected = false;
  49. break;
  50. }
  51. } else {
  52. BroadcastFilter bf = (BroadcastFilter)target;
  53. if (bf.requiredPermission == null) {
  54. allProtected = false;
  55. break;
  56. }
  57. }
  58. }
  59. if (allProtected) {
  60. // All safe!
  61. return;
  62. }
  63. }
  64. // The vast majority of broadcasts sent from system internals
  65. // should be protected to avoid security holes, so yell loudly
  66. // to ensure we examine these cases.
  67. if (callerApp != null) {
  68. Log.wtf(TAG, "Sending non-protected broadcast " + action
  69. + " from system " + callerApp.toShortString() + " pkg " + callerPackage,
  70. new Throwable());
  71. } else {
  72. Log.wtf(TAG, "Sending non-protected broadcast " + action
  73. + " from system uid " + UserHandle.formatUid(callingUid)
  74. + " pkg " + callerPackage,
  75. new Throwable());
  76. }
  77. }

如上代码, 对于广播,checkBroadcastFromSystem方法有三种检测。

  1. 如果Action 是Shell 发出的,则直接返回,不发出警告

  2. 如果Action 是protected-broadcast广播或一些指定的Action,不发出警告

  3. 如果Action 是指定了包名或者Compononent信息,只有特定应用能接收,则当所有receiver 均根据receiverPermisson 对权限进行了顾虑,则不发出警告

如果系统应用不满足以上三种情况,则发送公共广播时会被警告

如何解决 non-protected broadcast警告

从系统源码层面

1.在checkBroadcastFromSystem 处修改

  1. if (isProtectedBroadcast
  2. || Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
  3. || Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS.equals(action)
  4. || Intent.ACTION_MEDIA_BUTTON.equals(action)
  5. || Intent.ACTION_MEDIA_SCANNER_SCAN_FILE.equals(action)
  6. || Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS.equals(action)
  7. || Intent.ACTION_MASTER_CLEAR.equals(action)
  8. || Intent.ACTION_FACTORY_RESET.equals(action)
  9. || AppWidgetManager.ACTION_APPWIDGET_CONFIGURE.equals(action)
  10. || AppWidgetManager.ACTION_APPWIDGET_UPDATE.equals(action)
  11. || LocationManager.HIGH_POWER_REQUEST_CHANGE_ACTION.equals(action)
  12. || TelephonyIntents.ACTION_REQUEST_OMADM_CONFIGURATION_UPDATE.equals(action)
  13. || SuggestionSpan.ACTION_SUGGESTION_PICKED.equals(action)
  14. || AudioEffect.ACTION_OPEN_AUDIO_EFFECT_CONTROL_SESSION.equals(action)
  15. || AudioEffect.ACTION_CLOSE_AUDIO_EFFECT_CONTROL_SESSION.equals(action)) {
  16. // Broadcast is either protected, or it's a public action that
  17. // we've relaxed, so it's fine for system internals to send.
  18. return;
  19. }
  20. 复制代码

2.在/frameworks/base/services/core/java/com/android/server/pm/PackageManagerService.java#isProtectedBroadcast方法处修改

3.在/frameworks/base/core/res/AndroidManifest.xml 处增加 protected-broadcast 标签

在应用层面修改

  1. 在系统应用的 AndroidManifest.xml 处增加 protected-broadcast 标签,且apk放在 /system/priv-app 目录下

  2. 指定接收者.获取所有接收该广播的ResolveInfo,发送广播。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/443432
推荐阅读
相关标签
  

闽ICP备14008679号