在自己的应用中使用email的uri来查询数据,结果提示需要ACCESS_PROVIDER的权限(<uses-permission android:name="com.android.email.permission.ACCESS_PROVIDER" />),添加后运行依然提示此错误:
代码如下:
Cursor cur = mContext.getContentResolver().query(
Uri.parse("content://com.android.email.provider/message"),null,null,null,null);
导致错误的原因如下:
因为Android本身自带的email应用的保护级别很高,是android:protectionLevel="signatureOrSystem",根据SDK的文档来讲signatureOrSystem这种权限的应用第三方的应用是无法单独访问的,此种权限保护级别需要system本身镜像应用或者是同system镜像具有相同的签名应用才可以访问,因此一般的第三方应用即使添加了permission也还是会报错的。
解决方法:(生成system.img)
1 将自己的应用方到android源代码的packages/apps/目录下,添加Android.mk文件
2 在应用程序的AndroidManifest.xml中的manifest节点中加入
android:sharedUserId="android.uid.system"这个属性。
3 修改Android.mk文件,加入LOCAL_CERTIFICATE := platform这一行
4 使用mm命令来编译,生成的apk就有同system一样的权限了。
5 在android根目录下用make snod重新生成system.img
6 启动emulator运行下应用看看是否好了。
网上还有另外一种方法,就是用signapk的方式。
permission可分四个等级
android:protectionLevel="normal"
android:protectionLevel="dangerous"
android:protectionLevel="signature"
android:protectionLevel="signatureOrSystem"
normal
The default value. A lower-risk permission that gives requesting applications access to isolated application-level features, with minimal risk to other applications, the system, or the user. The system automatically grants this type of permission to a requesting application at installation, without asking for the user's explicit approval (though the user always has the option to review these permissions before installing).
dangerous
A higher-risk permission that would give a requesting application access to private user data or control over the device that can negatively impact the user. Because this type of permission introduces potential risk, the system may not automatically grant it to the requesting application. For example, any dangerous permissions requested by an application may be displayed to the user and require confirmation before proceeding, or some other approach may be taken to avoid the user automatically allowing the use of such facilities.
signature
A permission that the system grants only if the requesting application is signed with the same certificate as the application that declared the permission. If the certificates match, the system automatically grants the permission without notifying the user or asking for the user's explicit approval.
signatureOrSystem
A permission that the system grants only to applications that are in the Android system image or that are signed with the same certificates as those in the system image. Please avoid using this option, as the "signature" protection level should be sufficient for most needs and works regardless of exactly where applications are installed. The "signatureOrSystem" permission is used for certain special situations where multiple vendors have applications built into a system image and need to share specific features explicitly because they are being built together.