Exported service does not require permission问题。

今天在编辑一个简单的aidl的例子的时候遇到的一个小问题。原本编辑完后准备运行,无心中看到AndroidManifest.xml有个警告,内容为“Exported service does not require permission”.配置文件代码以下:android

1 <service android:name=".AidlService">
2             <intent-filter >
3                 <action android:name="com.example.myaidldemo.action.AIDL_SERVICE"/>
4             </intent-filter>
5         </service>

警告“Exported service does not require permission”的意思是“外部的service不须要权限”。既然不须要权限那么为何还会有警告呢???在网上搜了一些此类问题,也没说为何。不过却是按照他们的方法解决了问题。那就是直接在service中设置exported属性为“false”,限制外界访问。问题就解决了。以下:api

1 <service android:name=".AidlService"
2             android:exported="false">
3             <intent-filter >
4                 <action android:name="com.example.myaidldemo.action.AIDL_SERVICE"/>
5             </intent-filter>
6         </service>

其实回过头来想一想,其实只是本身犯了个很弱智的问题,由于本身和不少遇到这种问题的人同样,那就是:理解错误!由于之前学习你们遇到的一般都是没有加权限而致使的问题,因此天然而然的想到没加权限会报错,没法访问之类的。可是此次遇到的是“外部的service不须要权限”,其意思是:外部的其余service根本不须要添加权限就可以轻易地访问咱们编写的这个aidl,或者说这个service。这是出于安全性的考虑而给咱们的警告。而不是告诉咱们缺乏相应的权限。本身傻傻的还想require这个单词是否是还有其余意思,会不会有“须要”的意思,查有道,没有。翻牛津。仍是没有。最后甚至想是否是编辑文档的人写错了,把acquire错写成了require?最后才发现只是理解错了,习惯性思惟犯的错。安全

那么,问题又来了,android:exported="false"  这句不是系统默认的吗?怎么还要本身手动加上?难道系统的默认值是“true”?api文档中这么说的:app

android:exported 
Whether or not components of other applications can invoke the service or interact with it — " true" if they can, and " false" if not. When the value is " false", only components of the same application or applications with the same user ID can start the service or bind to it. 
The default value depends on whether the service contains intent filters. The absence of any filters means that it can be invoked only by specifying its exact class name. This implies that the service is intended only for application-internal use (since others would not know the class name). So in this case, the default value is "false". On the other hand, the presence of at least one filter implies that the service is intended for external use, so the default value is "true". 

This attribute is not the only way to limit the exposure of a service to other applications. You can also use a permission to limit the external entities that can interact with the service (see the permission attribute). 

关键的一句是:学习

The default value depends on whether the service contains intent filters.

就是说默认值取决于该service是否包含intent过滤器filter。若是没有<intent-filter>,那只能经过指定其准确的类名访问。这意味着该service打算只在该应用内部使用(由于其余应用可能根本不知道这个类的名字)。在这种状况下,默认值是“false”,另外一方面,当存在至少一个filter时,就代表该service打算供外部来使用,所以默认值是“true”。写到这里相信你们知道问什么了吧。原来api里面写的清清楚楚,看来api文档才是最好的资料这句话一点没错。是真正的宗。ui

其实还有一种解决的办法。那就是咱们在这里本身定义一个权限。既然外部的其余service不需权限就能访问咱们的service,那么咱们本身定义权限,而后在须要调用该service的应用的配置文件里面声明调用该service的所须要的权限不就能够了吗?代码更改成:this

1 <service android:name=".AidlService"
2             android:permission="com.exmaple.myaidldemo.myaidlservice">
3             <intent-filter >
4                 <action android:name="com.example.myaidldemo.action.AIDL_SERVICE"/>
5             </intent-filter>
6         </service>

而后在咱们须要调用该service的其余应用的配置文件中加入下面这行代码:spa

<uses-permission android:name="com.exmaple.myaidldemo.myaidlservice"/>

如今为止咱们就算是完整的就绝了遇到的问题了。最后感叹下,文档时个好东西啊。毕竟书里面不可能介绍的很是详细。只有多读文档才是正道。code

相关文章
相关标签/搜索