参考 http://teachcourse.cn/2319.html
参考http://blog.csdn.net/xyzjl/article/details/47192339 打开不同的设置界面
适配6.0手机Android:targetSdkVersion=”23”,选用高版本的V4包如果当前Activity继承自普通Activity,那么需要实现OnRequestPermissionsResultCallback接口,然后重写onRequestPermissionsResult方法拿到监听的结果,如果继承自FragmentActivity或者AppCompatactivity,因为已经实现了接口,不需要再实现
// 申请照相机权限
private void getPermission() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
// 申请CAMERA权限
ActivityCompat
.requestPermissions(this,
new String[] { Manifest.permission.CAMERA },
REQUEST_CAMERA);
} else {
takePic();
}
}
// 申请外部储存卡权限
private void getPermissions() {
if (ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
// 申请读取外部储存卡的权限
ActivityCompat
.requestPermissions(
this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
CODE_FOR_WRITE_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int requestCode,
String[] permissions, int[] grantResults) {
// TODO Auto-generated method stub
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
doNext(requestCode, permissions, grantResults);
}
private void doNext(int requestCode, String[] permissions,
int[] grantResults) {
if (requestCode == REQUEST_CAMERA) {
if (grantResults.length != 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 用户同意使用照相机
takePic();
} else if (grantResults.length != 0&&grantResults[0] != PackageManager.PERMISSION_GRANTED) {
boolean isSecondRequest = ActivityCompat
.shouldShowRequestPermissionRationale(this,
permissions[0]);
if (isSecondRequest) {
/** 重新请求授予权限,显示权限说明(该说明属于系统UI内容,区别第一次弹框) 用户点击禁止以后,再次弹出提示框**/
ActivityCompat.requestPermissions(this,
new String[] { Manifest.permission.CAMERA },
REQUEST_CAMERA);
} else {
//用户点击了禁止以后不再询问,下次打开的时候跳转到设置界面,让用户手动打开权限
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
SDToast.showToast("拍照权限被禁用,请在权限管理修改");
}
}
}
if (requestCode == CODE_FOR_WRITE_PERMISSION) {
// permissions[0]==Manifest.permission.WRITE_EXTERNAL_STORAGE&&grantResults[0]
// == PackageManager.PERMISSION_GRANTED
if (grantResults.length != 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// 用户同意使用照相机
} else if (grantResults.length != 0&&grantResults[0] != PackageManager.PERMISSION_GRANTED) {
boolean isSecondRequest = ActivityCompat
.shouldShowRequestPermissionRationale(this,
permissions[0]);
if (isSecondRequest) {
/** 重新请求授予权限,显示权限说明(该说明属于系统UI内容,区别第一次弹框) **/
ActivityCompat
.requestPermissions(
this,
new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE },
CODE_FOR_WRITE_PERMISSION);
} else {
// // 用户不同意使用照相机
// SDToast.showToast("不同意保存照片");
// Log.i(TAG, "不同意保存照片");
Intent intent = new Intent(
Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivityForResult(intent, REQUEST_PERMISSION_SETTING);
SDToast.showToast("访问SD卡权限被禁用,请在权限管理修改");
}
}
}
}