700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > Android -- 自定义权限

Android -- 自定义权限

时间:2019-02-03 10:40:43

相关推荐

Android -- 自定义权限

在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序,系统或者用户带来负面影响的操作。如果应用需要执行某些操作,就需要声明使用这个操作对应的权限。 (在manifest文件中 添加标记)。

app可以自定义属于自己的permission 或属于开发者使用的同一个签名的permission。定义一个permission 就是在menifest文件中添加一个permission标签。

<permission android:description="string resource" android:icon="drawable resource" android:label="string resource" android:name="string" android:permissionGroup="string" android:protectionLevel=["normal" | "dangerous" | "signature" | "signatureOrSystem"] />

android:description :对权限的描述,一般是两句话,第一句话描述这个权限所针对的操作,第二句话告诉用户授予app这个权限会带来的后果android:label: 对权限的一个简短描述android:name :权限的唯一标识,一般都是使用 报名加权限名android:permissionGroup: 权限所属权限组的名称android:protectionLevel: 权限的等级。normal 是最低的等级,声明次权限的app,系统会默认授予次权限,不会提示用户;dangerous 权限对应的操作有安全风险,系统在安装声明此类权限的app时会提示用户;signature 权限表明的操作只针对使用同一个证书签名的app开放;signatureOrSystem 与signature类似,只是增加了rom中自带的app的声明

android:name 属性是必须的,其他的可选,未写的系统会指定默认值

Code

首先创建了两个app,app A ,app B ;app A中注册了一个BroadcastReceiver ,app B 发送消息

app A的menifest文件:

<manifest xmlns:android="/apk/res/android" package="com.example.testbutton" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <!-- 声明权限 --> <permission android:name="com.example.testbutton.RECEIVE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" launcheMode="singleTask" android:configChanges="locale|orientation|keyboardHidden" android:screenOrientation="portrait" android:theme="@style/android:style/Theme.NoTitleBar.Fullscreen" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <!-- 注册Broadcast Receiver,并指定了给当前Receiver发送消息方需要的权限 --> <receiver android:name="com.example.testbutton.TestButtonReceiver" android:permission="com.example.testbutton.RECEIVE" > <intent-filter> <action android:name="com.test.action" /> </intent-filter> </receiver> </application> </manifest>

app B 的menifest 文件内容:

<manifest xmlns:android="/apk/res/android" package="com.example.testsender" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="7" android:targetSdkVersion="15" /> <!-- 声明使用指定的权限 --> <uses-permission android:name="com.example.testbutton.RECEIVE" /> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".MainActivity" android:label="@string/title_activity_main" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>

这样app B 给app A 发送消息,A就可以收到了,若未在app B的menifest文件中声明使用相应的权限,app B发送的消息,A是收不到的。

同样应用于Activity等组件。

另外,也可在app B 的menifest文件中声明权限时,添加android:protectionLevel="signature",指定app B只能接收到使用同一证书签名的app 发送的消息。

我是天王盖地虎的分割线

参考:/blog/1782854

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。