Android6.0 动态权限 笔录
2016-09-08 本文已影响311人
PeOS
导航:
- Android6.0 动态权限申请程序
- 普通权限与动态权限
- 完整代码
在Android6.0中,系统权限被分为两大类:一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如读取sdcard、访问通讯录等。
一、Android6.0 动态权限申请程序
当我们的Android工程的targetSdkVersion大于等于23的时候,对于Dangerous Permission,我们就需要手动编写逻辑代码实现权限的授权申请。
以申请相机权限为例,首先,需要在配置文件AndroidManifest中申请我们需要的权限:
<!-- 相机 -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.hardware.camera.autofocus" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.BROADCAST_STICKY" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
第二步,我在需要使用相机权限的地方进行权限申请操作。首先判断当前系统是否为Android6.0或之上的版本,如果不是,直接直接执行下一步,否则进入权限申请程序:
首先,checkSelfPermission检查该权限是否已经申请;
shouldShowRequestPermissionRationale检查用户是否已经拒绝过一次了,再次弹出授权框询问并解释为什么需要这个权限;
ActivityCompat.requestPermissions进行权限申请,申请的结果在onRequestPermissionsResult中接收。
//SDK版本为Android6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//检查权限
int checkPermissionCamera = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
if(checkPermissionCamera != PackageManager.PERMISSION_GRANTED){
Log.d(Tag, "权限还没有申请");
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA)){
Log.d(Tag, "用户已经拒绝一次了,再次弹出授权框询问并解释为什么需要这个权限");
}
else {
//申请授权
Log.d(Tag, "申请授权");
ActivityCompat.requestPermissions(MainActivity.this
//要申请的权限
, new String[]
{
Manifest.permission.CAMERA
}
//申请请求返回码
, 100);
}
}
else{
Log.d(Tag, "权限已经申请");
}
}
onRequestPermissionsResult:
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
switch (requestCode){
case 100:{
if(permissions[0].equals(Manifest.permission.CAMERA) && grantR
![1.png](http:https://img.haomeiwen.com/i797252/49f05105989697ed.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)esults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(Tag, "CAMERA 权限授权成功");
}
else{
Log.d(Tag, "用户拒绝授权权限 CAMERA");
}
break;
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
各种情况下的程序运行流程:
1、已经授权:
1.png
2、未授权,授权框点击允许:
2.png
3、未授权,授权框点击拒绝:
3.png
二、普通权限与动态权限
1、普通权限
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
2、危险权限
三、完整代码
package com.peos.permissiondemo;
import android.Manifest;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private static final String Tag = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
((Button)findViewById(R.id.btn_signle)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//SDK版本为Android6.0以上
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//检查权限
int checkPermissionCamera = ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.CAMERA);
if(checkPermissionCamera != PackageManager.PERMISSION_GRANTED){
Log.d(Tag, "权限还没有申请");
if (ActivityCompat.shouldShowRequestPermissionRationale(MainActivity.this,
Manifest.permission.CAMERA)){
Log.d(Tag, "用户已经拒绝一次了,再次弹出授权框询问并解释为什么需要这个权限");
}
else {
//申请授权
Log.d(Tag, "申请授权");
ActivityCompat.requestPermissions(MainActivity.this
//要申请的权限
, new String[]
{
Manifest.permission.CAMERA
}
//申请请求返回码
, 100);
}
}
else{
Log.d(Tag, "权限已经申请");
}
}
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults){
switch (requestCode){
case 100:{
if(permissions[0].equals(Manifest.permission.CAMERA) && grantResults[0] == PackageManager.PERMISSION_GRANTED){
Log.d(Tag, "CAMERA 权限授权成功");
}
else{
Log.d(Tag, "用户拒绝授权权限 CAMERA");
}
break;
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}