运行时权限

一、概念

系统权限根据敏感程度分为普通权限和危险权限两类。两类权限都需要在AndroidManifest.xml文件中申明。在Android 5.1 (API level 22) 及其以下,系统在APP安装时要求用户授权所有权限,否则APP不能安装;而在Android 6.0及其以上版本上,系统在APP安装时授权所有普通权限,危险权限需要在使用时动态让用户授权。这使得Android的权限管理更加灵活,用户可以根据需要在设置应用中对应用的各个危险权限授予不同的权限。

二、普通权限

android.permission.ACCESS_LOCATION_EXTRA_COMMANDS
android.permission.ACCESS_NETWORK_STATE
android.permission.ACCESS_NOTIFICATION_POLICY
android.permission.ACCESS_WIFI_STATE
android.permission.ACCESS_WIMAX_STATE
android.permission.BLUETOOTH
android.permission.BLUETOOTH_ADMIN
android.permission.BROADCAST_STICKY
android.permission.CHANGE_NETWORK_STATE
android.permission.CHANGE_WIFI_MULTICAST_STATE
android.permission.CHANGE_WIFI_STATE
android.permission.CHANGE_WIMAX_STATE
android.permission.DISABLE_KEYGUARD
android.permission.EXPAND_STATUS_BAR
android.permission.FLASHLIGHT
android.permission.GET_ACCOUNTS
android.permission.GET_PACKAGE_SIZE
android.permission.INTERNET
android.permission.KILL_BACKGROUND_PROCESSES
android.permission.MODIFY_AUDIO_SETTINGS
android.permission.NFC
android.permission.READ_SYNC_SETTINGS
android.permission.READ_SYNC_STATS
android.permission.RECEIVE_BOOT_COMPLETED
android.permission.REORDER_TASKS
android.permission.REQUEST_INSTALL_PACKAGES
android.permission.SET_TIME_ZONE
android.permission.SET_WALLPAPER
android.permission.SET_WALLPAPER_HINTS
android.permission.SUBSCRIBED_FEEDS_READ
android.permission.TRANSMIT_IR
android.permission.USE_FINGERPRINT
android.permission.VIBRATE
android.permission.WAKE_LOCK
android.permission.WRITE_SYNC_SETTINGS
com.android.alarm.permission.SET_ALARM
com.android.launcher.permission.INSTALL_SHORTCUT
com.android.launcher.permission.UNINSTALL_SHORTCUT

三、危险权限

//日历
public static final String[] CALENDAR_GROUP = {Manifest.permission.READ_CALENDAR, Manifest.permission.WRITE_CALENDAR};
//摄像头
public static final String[] CAMERA_GROUP = {Manifest.permission.CAMERA};
//联系人
public static final String[] CONTACTS_GROUP = {Manifest.permission.READ_CONTACTS, Manifest.permission.WRITE_CONTACTS, Manifest.permission.GET_ACCOUNTS};
//位置
public static final String[] LOCATION_GROUP = {Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
//话筒
public static final String[] MICROPHONE_GROUP = {Manifest.permission.RECORD_AUDIO};
//电话
public static final String[] PHONE_GROUP = {Manifest.permission.READ_PHONE_STATE, Manifest.permission.CALL_PHONE, Manifest.permission.READ_CALL_LOG, Manifest.permission.WRITE_CALL_LOG, Manifest.permission.ADD_VOICEMAIL, Manifest.permission.USE_SIP, Manifest.permission.PROCESS_OUTGOING_CALLS};
//传感器
public static final String[] SENSORS_GROUP = {Manifest.permission.BODY_SENSORS};
//短信
public static final String[] SMS_GROUP = {Manifest.permission.SEND_SMS, Manifest.permission.RECEIVE_SMS, Manifest.permission.READ_SMS, Manifest.permission.RECEIVE_WAP_PUSH, Manifest.permission.RECEIVE_MMS};
//存储
public static final String[] STORAGE_GROUP = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

四、例子

//build.gradle(project)
// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        maven {
            name 'AliYun Google Repository Proxy'
            url 'https://maven.aliyun.com/repository/google'
        }
        maven {
            name 'AliYun Jcenter Repository Proxy'
            url 'https://maven.aliyun.com/repository/jcenter'
        }
//        google()
//        jcenter()
    }

    dependencies {
        classpath 'com.android.tools.build:gradle:3.2.1'
        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        maven {
            name 'AliYun Google Repository Proxy'
            url 'https://maven.aliyun.com/repository/google'
        }
        maven {
            name 'AliYun Jcenter Repository Proxy'
            url 'https://maven.aliyun.com/repository/jcenter'
        }
//        google()
//        jcenter()
    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

//build.gradle(module)
apply plugin: 'com.android.application'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.tomorrow.architetest"
        minSdkVersion 15
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:support-compat:28.0.0'

    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

//AndroidManifest.xml


    
    
        
            
                
                
            
        
    


//MainActivity
package com.tomorrow.architetest;

import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = "MainActivity";
    private static final int REQUEST_PERMISSION_STORAGE = 10;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "zwm, onCreate");
        setContentView(R.layout.activity_main);

        if(ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
            Log.d(TAG, "zwm, READ_EXTERNAL_STORAGE permission get");
        } else {
            Log.d(TAG, "zwm, READ_EXTERNAL_STORAGE permission not get, now request");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_PERMISSION_STORAGE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.d(TAG, "zwm, onRequestPermissionsResult, requestCode: " + requestCode);
        if(requestCode == REQUEST_PERMISSION_STORAGE) {
            if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                Log.d(TAG, "zwm, permission get");
            } else if(ActivityCompat.shouldShowRequestPermissionRationale(this, permissions[0])) {
                Log.d(TAG, "zwm, permission not get, shouldShowRequestPermissionRationale true");
            } else {
                Log.d(TAG, "zwm, permission not get, shouldShowRequestPermissionRationale false");
            }
        }
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    }
}

//输出log(路径:启动MainActivity -> 拒绝 -> 退出 -> 启动MainActivity -> 点击[拒绝]后不在提示 -> 拒绝 -> 退出 -> 启动MainActivity)
2019-11-22 14:53:37.181 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onCreate
2019-11-22 14:53:37.310 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, READ_EXTERNAL_STORAGE permission not get, now request
2019-11-22 14:53:41.022 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onRequestPermissionsResult, requestCode: 10
2019-11-22 14:53:41.023 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, permission not get, shouldShowRequestPermissionRationale true
2019-11-22 14:53:46.327 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onCreate
2019-11-22 14:53:46.363 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, READ_EXTERNAL_STORAGE permission not get, now request
2019-11-22 14:53:52.554 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onRequestPermissionsResult, requestCode: 10
2019-11-22 14:53:52.555 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, permission not get, shouldShowRequestPermissionRationale false
2019-11-22 14:53:57.450 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onCreate
2019-11-22 14:53:57.488 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, READ_EXTERNAL_STORAGE permission not get, now request
2019-11-22 14:53:57.806 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, onRequestPermissionsResult, requestCode: 10
2019-11-22 14:53:57.807 16067-16067/com.tomorrow.architetest D/MainActivity: zwm, permission not get, shouldShowRequestPermissionRationale false

你可能感兴趣的:(运行时权限)