从Android 6.0开始,有部分危险权限申请需要在运行时候申请,一般是你的目标版本targetSdkVersion大于或等于23就要动态申请权限了,否则APP在运行当中会崩溃。
一、基础知识
1、权限种类
权限分两种:正常权限和危险权限。危险权限在targetSdkVersion大于或等于23就要动态申请权限了。危险权限有以下几种:
权限组权限
CALENDAR
READ_CALENDAR
WRITE_CALENDAR
CAMERA
CAMERA
CONTACTS
READ_CONTACTS
WRITE_CONTACTS
GET_ACCOUNTS
LOCATION
ACCESS_FINE_LOCATION
ACCESS_COARSE_LOCATION
MICROPHONE
RECORD_AUDIO
WRITE_CALENDAR
PHONE
READ_PHONE_STATE
CALL_PHONE
READ_CALL_LOG
WRITE_CALL_LOG
ADD_VOICEMAIL
USE_SIP
PROCESS_OUTGOING_CALLS
SENSORS
BODY_SENSORS
SMS
SEND_SMS
RECEIVE_SMS
READ_SMS
RECEIVE_WAP_PUSH
RECEIVE_MMS
STORAGE
READ_EXTERNAL_STORAGE
WRITE_EXTERNAL_STORAGE
每组权限中,用户只要授权该组下的一个权限,该组中所有权限都可以用。
2、权限相关
是否需要向用户解析需要申请的权限。
ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)
如果应用之前请求过此权限但用户拒绝了请求,此方法将返回 true。
如果用户在过去拒绝了权限请求,并在权限请求系统对话框中选择了 Don’t ask again 选项,此方法将返回 false。
如果设备规范禁止应用具有该权限,此方法也会返回 false。所以第一次进入APP申请该权限时,有些设备会返回false(第二次申请才为true)。
检查APP是否已经有权限。
ContextCompat.checkSelfPermission(thisActivity, Manifest.permission.READ_CONTACTS)
返回PackageManager.PERMISSION_GRANTED表示已拥有,不具有则返回PackageManager.PERMISSION_DENIED
权限申请
ActivityCompat.requestPermissions(thisActivity,
new String[]{Manifest.permission.READ_CONTACTS},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
ActivityCompat.requestPermissions用于申请权限。
thisActivity,不用多说了,就是当前的Activity名字的一个实例(引用)。
new String[]{Manifest.permission.READ_CONTACTS}是你要申请的权限,你可以写几个权限进入数组里申请。
MY_PERMISSIONS_REQUEST_READ_CONTACTS为你自己定义一个对应的号码,方便系统回调时识别。
处理权限请求响应
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
}
这里处理的是权限相应结果。
requestCode对应之前的你自己定义的号码MY_PERMISSIONS_REQUEST_READ_CONTACTS。
grantResults是权限申请结果数组。其结果可以是PackageManager.PERMISSION_GRANTED或PackageManager.PERMISSION_DENIED
跳转到用户权限管理页面
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Log.d(TAG, "getPackageName(): " + activity.getPackageName());
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
AndroidManifest里填写
这里填写的权限和普通的权限相差无异。
二、权限申请
单权限申请
下面来实现一个单权限的申请。
package com.example.test;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements ActivityCompat.OnRequestPermissionsResultCallback {
private static String TAG = "MainActivity";
View mLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = findViewById(R.id.activity_main);
}
@Override
public boolean shouldShowRequestPermissionRationale(String permission) {
return super.shouldShowRequestPermissionRationale(permission);
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
openSettingActivity(this, "没有此权限,无法开启这个功能,请开启权限。PERMISSION_READ_ACCOUNTS 和 PERMISSION_GET_ACCOUNTS" );
}
}
private static void openSettingActivity(final Activity activity, String message) {
showMessageOKCancel(activity, message, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent();
intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Log.d(TAG, "getPackageName(): " + activity.getPackageName());
Uri uri = Uri.fromParts("package", activity.getPackageName(), null);
intent.setData(uri);
activity.startActivity(intent);
}
});
}
private static void showMessageOKCancel(final Activity context, String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(context)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
public void showContacts(View v) {
Log.i(TAG, "Show contacts button pressed. Checking permissions.");
//检查是否够权限
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED
|| ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
Log.i(TAG, "Contact permissions has NOT been granted. Requesting permissions.");
requestContactsPermissions();
}
}
private static String[] PERMISSIONS_CONTACT = {Manifest.permission.READ_CONTACTS,
Manifest.permission.WRITE_CONTACTS};
/**
* Id to identify a contacts permission request.
*/
private static final int REQUEST_CONTACTS = 1;
/**
* 申请权限
*/
private void requestContactsPermissions() {
//检查是否用户点击了不再提示选项
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.READ_CONTACTS)
|| ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_CONTACTS)) {
Log.i(TAG, "Displaying contacts permission rationale to provide additional context.");
//提示信息
Snackbar.make(mLayout, "Contacts permissions are needed to demonstrate access.", Snackbar.LENGTH_INDEFINITE)
.setAction("ok", new View.OnClickListener() {
@Override
public void onClick(View view) {
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
}
})
.show();
} else {
// Contact permissions have not been granted yet. Request them directly.
ActivityCompat.requestPermissions(this, PERMISSIONS_CONTACT, REQUEST_CONTACTS);
}
}
}
权限申请清单
实现流程:
1.检查是否有该权限
2.检查是否解析受限,如果有则直接申请权限,没有则弹窗提示用户申请权限。
3.系统回调onRequestPermissionsResult反馈申请结果。
4.如果用户拒绝就弹窗提示是否跳转到设置里面的应用软件权限设置页面。
多权限申请
多权限申请和单权限申请类似,在申请时候申请多个而已。
List requestPermissions = new ArrayList();
requestPermissions.add(Manifest.permission.RECORD_AUDIO);
requestPermissions.add(Manifest.permission.GET_ACCOUNTS);
requestPermissions.add(Manifest.permission.READ_PHONE_STATE);
requestPermissions.add(Manifest.permission.CALL_PHONE);
requestPermissions.add(Manifest.permission.CAMERA);
requestPermissions.add(Manifest.permission.ACCESS_FINE_LOCATION);
requestPermissions.add(Manifest.permission.ACCESS_COARSE_LOCATION);
requestPermissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);
requestPermissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
ActivityCompat.requestPermissions(MainActivity.this, requestPermissions.toArray(new String[requestPermissions.size()]), REQUEST_CONTACTS);
记得在AndroidManifest里填写对应的权限。
本文Demo到Linux公社资源站下载:
------------------------------------------分割线------------------------------------------
具体下载目录在 /2017年资料/1月/17日/Android 6.0动态权限申请/
------------------------------------------分割线------------------------------------------