android:exported=”false”是什么意思

android:exported="false" 出现在 四大组件(Activity / Service / Receiver / Provider)的清单声明里,它的含义是:

“此组件不允许被本应用以外的任何进程直接调用。”


✅ 具体行为

场景 结果
别的 App 用 Intent 启动这个 Activity 直接失败(ActivityNotFoundException 或隐式匹配不到)
别的 App 用 startService/bindService 连接这个 Service 连接失败
别的 App 用 sendBroadcast 给这个 Receiver 收不到
别的 App 用 ContentResolver 访问这个 Provider 访问失败(除非额外声明 grant-uri-permission 等)


✅ 为什么要显式写
从 API 31(Android 12)开始,只要组件带 intent-filter,就必须同时写明 android:exported="true|false",否则编译期直接报错:

Targeting S+ (version 31 and above) requires that an explicit value for android:exported be defined ...

✅ 什么时候该用

用途 建议值
仅 App 内部自己用的 Activity/Service/Receiver/Provider android:exported="false"(默认更安全)
需要对外暴露(如 Launcher Activity、对外 SDK Service) android:exported="true"


✅ 示例

<service
    android:name=".MyInternalService"
    android:exported="false" />   <!-- 只有本应用可启动 -->

<activity
    android:name=".MainActivity"
    android:exported="true">      <!-- Launcher 必须暴露 -->
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

✅ 一句话总结

android:exported="false" 就是给组件加一把“对外上锁”,让它只能被自己人调用,别人想都别想。

暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇