android:exported ="true"对于身份验证服务真的有必要吗?

Rob*_*ley 22 authentication android android-manifest

实现Android身份验证器通常涉及两项服务 - 用于返回身份验证器的身份验证服务,以及提供同步适配器的同步服务.这个问题具体是关于身份验证服务,尽管在大多数示例中, 两个服务都被赋予了android:exported="true"属性AndroidManifest.xml,例如:

<service
    android:name=".authenticator.AuthenticationService"
    android:exported="true">
    <intent-filter>
        <action
            android:name="android.accounts.AccountAuthenticator" />
    </intent-filter>
    <meta-data
        android:name="android.accounts.AccountAuthenticator"
        android:resource="@xml/authenticator" />
</service>
Run Code Online (Sandbox Code Playgroud)

从身份验证服务中删除属性似乎没有效果(测试Froyo,Gingerbread) - 身份验证代码继续正常工作 - 标志实际上是否必要?

Rob*_*ley 38

好的,要通过阅读文档自己回答这个问题,该属性的文档exported说:

默认值取决于服务是否包含意图过滤器.没有任何过滤器意味着只能通过指定其确切的类名来调用它.这意味着该服务仅供应用程序内部使用(因为其他人不知道类名).所以在这种情况下,默认值为"false".另一方面,至少一个过滤器的存在意味着该服务旨在供外部使用,因此默认值为"true".

所有身份验证服务都有一个intent过滤器 - AbstractAccountAuthenticator的文档说:

为了成为一个身份验证者,必须...编写一个服务,当使用Action ACTION_AUTHENTICATOR_INTENT调用intent时,在服务的onBind(android.content.Intent)中返回getIBinder()的结果.

这需要一个intent过滤器,因此为服务导出的默认值为true.所以这个问题的答案是"不,这个属性没有必要 - 因为它默认是真的".