android6.0出来也有好一阵子了,现在市面的部分手机已经开始搭载android7.0+的系统了,考虑到新系统市场占有率越来越多,还是要把6.0的权限问题“亡羊补牢”一下吧。
新的权限机制更好的保护了用户的隐私,Google将权限分为两类,一类是Normal Permissions,这类权限一般不涉及用户隐私,是不需要用户进行授权的,比如手机震动、访问网络等;另一类是Dangerous Permission,一般是涉及到用户隐私的,需要用户进行授权,比如打电话,读取sdcard、访问通讯录等。这里具体说一下Dangerous权限。
可以发现危险权限是分组的,总共有9组,同一组的任何一个权限被授权了,其他权限也自动被授权。例如,一旦WRITE_CONTACTS被授权了,app也就同时获得了READ_CONTACTS和GET_ACCOUNTS的权限了。
我们不但需要在代码中动态请求,还要事先在AndroidManifest.xml文件中声明我们需要使用的权限,代码中动态去请求一个未声明的权限,App会崩溃掉的。
AndroidManifest.xml内容:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.shi.androidstudio.permission">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
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>
<activity android:name=".CameraActivity" />
<activity android:name=".MultiPermissionActivity"/>
application>
manifest>
申请打开照相机的权限并打开照相机CameraActivity.java代码:
package com.shi.androidstudio.permission;
import android.Manifest;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.provider.Settings;
import android.support.annotation.NonNull;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class CameraActivity extends AppCompatActivity {
int MY_PERMISSIONS_REQUEST_READ_CONTACTS = 1;
private Button btn_openCamera;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
btn_openCamera = (Button) findViewById(R.id.btn_openCamera);
btn_openCamera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ContextCompat.checkSelfPermission(CameraActivity.this,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
//正常申请权限
ActivityCompat.requestPermissions(CameraActivity.this,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_READ_CONTACTS);
}else{
ToastUtil.show(CameraActivity.this, "已经授予权限");
openCamera();
}
}
});
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(CameraActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
}
if(requestCode == MY_PERMISSIONS_REQUEST_READ_CONTACTS){
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openCamera();
}else{
//如果app之前请求过该权限,被用户拒绝, 这个方法就会返回true.
//如果用户之前拒绝权限的时候勾选了对话框中”Don’t ask again”的选项,那么这个方法会返回false.
//如果设备策略禁止应用拥有这条权限, 这个方法也返回false.
//默认的,第一次打开APP,这个方法也返回false
boolean flag = ActivityCompat.shouldShowRequestPermissionRationale(CameraActivity.this,
Manifest.permission.CAMERA);
if (!flag) {
showMessageOKCancel("请授权", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// 用户拒绝授权并选择了不再提示
ToastUtil.show(CameraActivity.this,"请进行手动授权");
// 帮跳转到该应用的设置界面,让用户手动授权
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
});
}else{
SnackBarUtil.show(btn_openCamera, "授权失败");
}
}
}
}
/**
* 打开照相机
*/
void openCamera() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivity(intent);
}
}
MultiPermissionActivity.java代码:
package com.shi.androidstudio.permission;
import android.Manifest;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class MultiPermissionActivity extends AppCompatActivity {
int MY_PERMISSIONS_REQUEST_MULTI_CODE = 0;
Button btn_callPhone;
//被用户拒绝并勾选了不再提示选项
final List permissionsNeedRequest = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_multi_permission);
btn_callPhone = (Button) findViewById(R.id.btn_callPhone);
btn_callPhone.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getPermissions();
}
});
}
private void getPermissions(){
List list = new ArrayList<>();
list.add(Manifest.permission.READ_CONTACTS);
list.add(Manifest.permission.WRITE_CONTACTS);
list.add(Manifest.permission.CALL_PHONE);
list.add(Manifest.permission.ACCESS_FINE_LOCATION);
permissionRequest(list);
}
/**
* 检测用户是否勾选了不再提示选项,拒绝了返回false,没有拒绝返回true
**/
private void permissionRequest(List permissionsList) {
permissionsNeedRequest.clear();
for (int i=0; i//检测用户是否示已经授权该权限
if (ActivityCompat.checkSelfPermission( MultiPermissionActivity.this, permission) != PackageManager.PERMISSION_GRANTED) {
permissionsNeedRequest.add(permission);
}
}
if(permissionsNeedRequest.size() > 0){
ActivityCompat.requestPermissions( MultiPermissionActivity.this, permissionsNeedRequest.toArray(new String[permissionsNeedRequest.size()]),
MY_PERMISSIONS_REQUEST_MULTI_CODE);
}else{
Toast.makeText(MultiPermissionActivity.this, "权限全部申请成功了", Toast.LENGTH_SHORT)
.show();
}
}
private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
new AlertDialog.Builder(MultiPermissionActivity.this)
.setMessage(message)
.setPositiveButton("OK", okListener)
.setNegativeButton("Cancel", null)
.create()
.show();
}
// String message = "";
// for (int i = 0; i < permissionsNeedRequest.size(); i++){
// if(i == 0){
// message += "请授权" + permissionsNeedRequest.get(i);
// }else{
// message += ", " + permissionsNeedRequest.get(i);
// }
// }
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
if(MY_PERMISSIONS_REQUEST_MULTI_CODE == requestCode)
{
//初始化,防止NullPointerException
Map perms = new HashMap();
perms.put(Manifest.permission.CALL_PHONE, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.READ_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.WRITE_CONTACTS, PackageManager.PERMISSION_GRANTED);
perms.put(Manifest.permission.ACCESS_FINE_LOCATION, PackageManager.PERMISSION_GRANTED);
// Fill with results
for (int i = 0; i < permissions.length; i++)
perms.put(permissions[i], grantResults[i]);
// Check for ACCESS_FINE_LOCATION
if (perms.get(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.READ_CONTACTS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.WRITE_CONTACTS) == PackageManager.PERMISSION_GRANTED
&& perms.get(Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED)
{
// All Permissions Granted
Toast.makeText(MultiPermissionActivity.this, "权限全部申请成功了", Toast.LENGTH_SHORT)
.show();
} else {
// Permission Denied
Toast.makeText(MultiPermissionActivity.this, "部分权限被拒绝掉了", Toast.LENGTH_SHORT)
.show();
}
}
}
}