android framework中添加自定义权限

在android的framework源码中添加自定义的permission以便限制应用程序调用自定义的系统接口,
在frameworks/base/core/res/AndroidManifest.xml 中添加:
<permission android:name="android.permission.ENTERPRISE_SETTINGS"
        android:label="@string/permlab_invoke_ivt_method"
        android:description="@string/permdesc_invoke_ivt_method"
        android:protectionLevel="dangerous" />

在frameworks/base/core/res/values/string.xml中添加
permlab_invoke_ivt_method 、 permdesc_invoke_ivt_method的字符串资源

在被调用的接口处添加权限检验
public String getDeviceInfo() throws RemoteException 
{
 	。。。。。。
        mContext.enforceCallingOrSelfPermission("android.permission.ENTERPRISE_SETTINGS", null);
	。。。。。。
}

在调用该接口的应用程序的AndroidManifest.xml中需要添加该权限才可以正常调用该接口:
<uses-permission  android:name="android.permission.ENTERPRISE_SETTINGS"/>

其中的权限保护级别如下:
protectionLevel分为四级: 
"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. 

前面几个很好理解 
现在重点记忆下最后一个signatureOrSystem 顾名思义就是在拥有权限的同时还必须满足signature一致或System级别APK才拥有! 
现在做了如下尝试 

Test Result:
TestCustomPermission是我自定义了一个Activity的访问权限的APK
TestPermission 去访问TestCustomPermission的Activity


EclipseSignature 中两个都用eclipse的签名
OtherSignature 中两个都用相同的另一种签名
DifferentSignature 中两个签名不想同
以下是测试结果:

APP级别
权限设置为signatureOrSystem
1. EclipseSignature 成功访问 ! 可以加入权限!
2. OtherSignature 成功访问 ! 可以加入权限!
3. DifferentSignature  访问失败!


权限设置为normal
1. DifferentSignature   成功访问 ! 可以加入权限!


System 级别
权限设置为signatureOrSystem
1. EclipseSignature 成功访问 ! 可以加入权限!
2. OtherSignature 成功访问 ! 可以加入权限!
3. DifferentSignature  成功访问 ! 可以加入权限!


TestCustomPermission再 system TestPermission 在APP
1.DifferentSignature 失败
2.签名相同成功!


最后一个实验
在TestCustomPermission中注册 signatureOrSystem!APP层访问 在framework API中验证!
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Android Framework 添加自定义库,可以通过以下步骤实现: 1. 将自定义库的源代码添加Android Framework ,通常情况下位于 `frameworks/base/core/java` 或 `frameworks/base/services/core/java` 目录下。 2. 在 `frameworks/base/core/res` 或 `frameworks/base/services/core/res` 目录下添加自定义库的资源文件,比如布局文件、字符串资源等。 3. 在 Android.mk 文件添加自定义库的编译配置信息,包括源代码的编译和资源文件的打包。 4. 在 AndroidManifest.xml 文件注册自定义库,以便系统可以识别和加载该库。 5. 在 Java 代码使用 `import` 语句导入自定义库的类,并在代码调用该类的方法。 需要注意的是,自定义库需要在 APK 进行引用才能被 APK 调用。可以通过以下步骤将自定义库引用到 APK : 1. 在 APK 的 build.gradle 文件添加依赖库的引用,例如: ``` dependencies { implementation project(':mylibrary') } ``` 其 `mylibrary` 是自定义库的名称。 2. 在 APK 的 AndroidManifest.xml 文件添加自定义库的使用权限,例如: ``` <uses-library android:name="mylibrary" android:required="true" /> ``` 其 `mylibrary` 是自定义库的名称。 完成以上步骤后,自定义库就可以被 APK 调用了。需要注意的是,自定义库只能被具有相应权限的应用调用。如果需要让所有应用都能调用该库,可以考虑将库打包成系统级别的 APK,并将其放置到 `/system/app` 目录下。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值