在android系统的安全模型中,应用程序在默认的情况下不可以执行任何对其他应用程序、系统或者用户带来负面影响的操作。如果应用需要执行某些操作,就需要声明使用这个操作对应的权限。 (在manifest文件中 添加< uses-permission >标记)
当然,app也可以自定义属于自己的permission或属于开发者使用的同一个签名的permission。定义一个permission就是在manifest文件中添加一个permission标签。
<permission
android:name=""
android:description=""
android:banner=""
android:icon=""
android:label=""
android:logo=""
android:permissionFlags=""
android:permissionGroup=""
android:protectionLevel="" />
android:name => 权限的唯一标识,一般都是使用包名加权限名。(属性是必须的,其他的可选,未写的系统会指定默认值) 。
android:description => 对权限的描述,一般是两句话,第一句话描述这个权限所针对的操作,第二句话告诉用户授予app这个权限会带来的后果。
android:label => 对权限的一个简短描述。
android:permissionGroup => 权限所属权限组的名称。
android:protectionLevel => 权限等级。
权限等级 | 说明 |
---|---|
normal | 最低的等级,声明次权限的app,系统会默认授予次权限,不会提示用户 |
dangerous | 权限对应的操作有安全风险,系统在安装声明此类权限的app时会提示用户 |
signature | 权限表明的操作只针对使用同一个证书签名的app开放 |
signatureOrSystem | 与signature类似,只是增加了rom中自带的app的声明 |
下面创建了两个不同签名的app:SendBroadcastApplication 、ReceiveBroadcastApplication,通过指定一个BroadcastReceiver的权限来实验。
ReceiveBroadcastApplication 的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nicole.receivebroadcastappliaction">
<!-- 声明自定义的权限 -->
<permission android:name="com.nicole.receivebroadcastappliaction.RECEIVE"
android:protectionLevel="normal"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<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=".Receiver"
android:permission="com.nicole.receivebroadcastappliaction.RECEIVE" >
<intent-filter>
<action android:name="com.nicole.receivebroadcastappliaction.action" />
</intent-filter>
</receiver>
</application>
</manifest>
ReceiveBroadcastApplication 的 MainActivity
package com.nicole.receivebroadcastappliaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(){
@Override
public void run() {
super.run();
while (true){
try {
sleep(1000);
Log.e("====>","我在等...");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}.start();
}
}
ReceiveBroadcastApplication 的 Receiver
package com.nicole.receivebroadcastappliaction;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
/**
* Created by Nicole on 2016/11/8.
*/
public class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
Log.e("======>","收到广播");
}
}
ReceiveBroadcastApplication 的 build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.nicole.receivebroadcastappliaction"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
signingConfigs{
test {
storeFile file('./appkey')
storePassword 'appkey'
keyAlias 'appkey'
keyPassword 'appkey'
}
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
debug {
minifyEnabled false
signingConfig signingConfigs.test
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
SendBroadcastApplication 的 AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nicole.sendbroadcastapplication">
<!-- 声明要使用的权限 -->
<uses-permission android:name="com.nicole.receivebroadcastappliaction.RECEIVE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
SendBroadcastApplication 的 MainActivity
package com.nicole.sendbroadcastapplication;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.send).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.setAction("com.nicole.receivebroadcastappliaction.action");
sendBroadcast(intent);
}
});
}
}
SendBroadcastApplication 的 build.gradle
apply plugin: 'com.android.application'
android {
compileSdkVersion 24
buildToolsVersion "24.0.1"
defaultConfig {
applicationId "com.nicole.sendbroadcastapplication"
minSdkVersion 21
targetSdkVersion 24
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
compile 'com.android.support:appcompat-v7:24.2.1'
testCompile 'junit:junit:4.12'
}
测试结论:在ReceiveBroadcastApplication 的 AndroidManifest.xml 中
<!-- 声明自定义的权限 -->
<permission android:name="com.nicole.receivebroadcastappliaction.RECEIVE"
android:protectionLevel="normal"/>
protectionLevel=“normal” 的情况下,SendBroadcastApplication点击发送时,ReceiveBroadcastApplication会收到,而将“normal”更改为”signature”时,ReceiveBroadcastApplication就收不到了。原因signature表明操作只针对使用同一个证书签名的app开放。