今天在做修改公司BUG的时候,突然发现这个项目上没有Android6.0权限,以前都是在Android Studio上添加Android 6.0权限,这次是在Eclipse上添加Andorid 6.0权限。
总结一下自己遇到的坑:
一、相关API
checkSelfPermission() 检查权限
if(PermissionChecker.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { //没有权限去申请 }else{ //有权限 }
requestPermissions() 申请权限
ActivityCompat.requestPermissions(thisActivity, new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS);
该方法是异步的,第一个参数是Context;第二个参数是需要申请的权限的字符串数组;第三个参数为requestCode,主要用于回调的时候检测。可以从方法名requestPermissions以及第二个参数看出,是支持一次性申请多个权限的,系统会通过对话框逐一询问用户是否授权。
onRequestPermissionsResult() 处理权限结果回调
@Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { switch (requestCode) { case MY_PERMISSIONS: { //如果请求被取消了,结果数组是空的。 if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { //申请权限成功 } else { //申请权限失败 } return; } } }
首先验证requestCode定位到你的申请,然后验证grantResults对应于申请的结果,这里的数组对应于申请时的第二个权限字符串数组。如果你同时申请两个权限,那么grantResults的length就为2,分别记录你两个权限的申请结果。如果申请成功,就可以做你的事情了~
二、遇到的坑
targetSdkVersion 23以下6.0中调用checkSelfPermission有可能会找到不到
解决办法更换V4包
下载地址:http://download.csdn.net/detail/liusheng_/9903937更换V4包程序有可能运行不起来
解决办法更换V4包
更新SDK,使用最新的SDK
三、完整代码
最后已申请Android 6.0拍照权限为例
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener{
Button bt_Permissions;
private static final int MY_PERMISSIONS = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_Permissions= (Button) findViewById(R.id.bt_Permissions);
bt_Permissions.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (PermissionChecker.checkSelfPermission(MainActivity.this,
Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 申请权限,MY_PERMISSIONS是自定义的常量
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.CAMERA }, MY_PERMISSIONS);
} else {
// 有权限,直接拍照
}
}
//权限回调
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 申请成功,可以拍照
} else {
// 申请失败
}
return;
}
}
}
多个权限申请
import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.PermissionChecker;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements View.OnClickListener {
Button bt_Permissions;
private static final int MY_PERMISSIONS = 1;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt_Permissions = (Button) findViewById(R.id.bt_Permissions);
bt_Permissions.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String[] permissions = new String[]{Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE};
List<String> mPermissionList = new ArrayList<>();
//判断所有权限是否都授予
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(this, permissions[i]) != PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
if (mPermissionList.isEmpty()) {//未授予的权限为空,表示都授予了
} else {//请求权限方法
String[] newPermissions = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
ActivityCompat.requestPermissions(this, newPermissions, 1);
}
}
//权限回调
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
if (requestCode == MY_PERMISSIONS) {
int s = 0;//判断是否都已经授权
for (int i = 0; i < grantResults.length; i++) {
if (grantResults.length > 0
&& grantResults[i] == PackageManager.PERMISSION_GRANTED) {
} else {
s = s + 1;
}
}
if (s == 0) {//权限都已经申请成功
} else {//权限申请失败
}
return;
}
}
}