在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:name 属性是必须的,其余的可选,未写的系统会指定默认值app
首先建立了两个app,app A ,app B ; app A中注册了一个BroadcastReceiver ,app B 发送消息 code
app A的menifest文件: orm
<manifest xmlns:android="http://schemas.android.com/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 文件内容:xml
<manifest xmlns:android="http://schemas.android.com/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是收不到的。blog
一样应用于Activity等组件。 ip
另外,也可在app B 的menifest文件中声明权限时,添加android:protectionLevel="signature",指定app B只能接收到使用同一证书签名的app 发送的消息。 开发
参考:http://berdy.iteye.com/blog/1782854get