1、Activity中读取权限提示:
protected static final int REQUEST_CODE_LOCAL =2;
if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {//Android 6.0申请权限
ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_CODE_LOCAL);
}else{
//已获取权限,读取手机相册操作
selectPicFromLocal();
}
//重写方法 允许权限
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted 用户允许权限 继续执行
switch (requestCode) {
case REQUEST_CODE_LOCAL:
//已获取权限,读取手机相册操作
selectPicFromLocal();
break;
default:
break;
}
} else {
// Permission Denied 拒绝
MyApp.showToast("获取权限失败");
}
}
注意:Activity要实现回调接口ActivityCompat.
OnRequestPermissionsResultCallback,否则权限允许通过后不会执行onRequestPermissionsResult方法如图:
2、Fragment中读取权限提示:
protected static final int REQUEST_CODE_MAP = 1;
protected static final int REQUEST_CODE_CAMERA = 2;
protected static final int REQUEST_CODE_LOCAL = 3;
switch (itemId) {
case ITEM_TAKE_PICTURE:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {//Android 6.0申请权限
requestPermissions(new String[]{Manifest.permission.CAMERA},REQUEST_CODE_CAMERA);//Fragment中请求方式
// ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.CAMERA},REQUEST_CODE_CAMERA);//Activity中请求方式
return;
}else{
selectPicFromCamera();
}
break;
case ITEM_PICTURE:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {//Android 6.0申请权限
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_CODE_LOCAL);
// ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_CODE_LOCAL);
}else{
selectPicFromLocal();
}
break;
case ITEM_LOCATION:
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED) {//Android 6.0申请权限
requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_CODE_LOCAL);
// ActivityCompat.requestPermissions(getActivity(),new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},REQUEST_CODE_LOCAL);
}else{
startActivityForResult(new Intent(getActivity(), EaseBaiduMapActivity.class), REQUEST_CODE_MAP);
}
break;
default:
break;
}
}
/**
* 无论是否准许权限都会执行,但必须实现ActivityCompat.OnRequestPermissionsResultCallback
* @param requestCode
* @param permissions
* @param grantResults
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
Log.i("permission","--requestCode:"+requestCode+" permissions:"+permissions.toString()+" grantResults:"+grantResults);
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// Permission Granted 用户允许权限 继续执行
switch (requestCode) {
case REQUEST_CODE_CAMERA:
selectPicFromCamera();
break;
case REQUEST_CODE_LOCAL:
selectPicFromLocal();
break;
case REQUEST_CODE_MAP:
startActivityForResult(new Intent(getActivity(), EaseBaiduMapActivity.class), REQUEST_CODE_MAP);
break;
default:
}
} else {
// Permission Denied 拒绝
Toast.makeText(getActivity().getApplicationContext(),"获取权限失败",Toast.LENGTH_SHORT);
}
// super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
注意:
Fragment要实现回调接口ActivityCompat.OnRequestPermissionsResultCallback,否则权限允许通过后不会执行onRequestPermissionsResult方法
Activity和Fragment中使用requestPermissions方法不一样,Fragment使用Activity的方法的话,就不会执行onRequestPermissionsResult方法了
注意:代码中获取权限的方法也必须在AndroidManifest.xml中声明需要的权限;
要实现回调接口ActivityCompat.OnRequestPermissionsResultCallback;
参考:http://blog.csdn.net/yangqingqo/article/details/48371123