Android 6.0 动态权限申请 项目步骤

2017-11-25  本文已影响0人  长耳

1.在第一次进入项目 进行全部的危险权限的申请

//危险权限动态请求 并且第一次登录 进行验证 不去处理申请后的操作

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.JELLY_BEAN&&isFirst) {

//内存卡读写权限 以及 相机

String[] mPermissionList = {Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE};

ActivityCompat.requestPermissions(this, mPermissionList,REQUEST_STORAGE_READ_ACCESS_PERMISSION);

}

2.在需要申请权限的地方进行权限判定 如果未进行同意 进行重复申请 如果是点击不再提醒 并且权限拒绝 dialog提醒进入Setting界面

//qq分享需要内存卡权限

if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.JELLY_BEAN

&& ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)

!= PackageManager.PERMISSION_GRANTED) {

dynamicRequestPermission("QQ分享需要得到您的内存卡存储权限",

REQUEST_STORAGE_READ_ACCESS_PERMISSION, Manifest.permission.READ_EXTERNAL_STORAGE);

}else{

    //TODO share 2 qq

}

弹窗提醒 

private void dynamicRequestPermission(String rationale,final int requestCode,final String permission) {

if(ActivityCompat.shouldShowRequestPermissionRationale(this, permission)) {

newMaterialDialog.Builder(SharePublicLiveActivity.this)

.title(R.string.mis_permission_dialog_title)

.content(rationale)

.positiveText(R.string.mis_permission_dialog_ok)

.negativeText(R.string.mis_permission_dialog_cancel)

.onAny(newMaterialDialog.SingleButtonCallback() {

@Override

public voidonClick(@NonNullMaterialDialog dialog,@NonNullDialogAction which) {

if(which == DialogAction.NEGATIVE) {

dialog.dismiss();

}else if(which == DialogAction.POSITIVE) {

ActivityCompat.requestPermissions(SharePublicLiveActivity.this,newString[]{permission},requestCode);

}

}

})

.show();

}else{//我在首次启动已经都进项请求了 去设置界面了

goSetting();

}

}

//权限回调

@Override

public void onRequestPermissionsResult(int requestCode,@NonNull final String[]  permissions,@NonNull int[] grantResults) {

if(requestCode ==REQUEST_STORAGE_READ_ACCESS_PERMISSION) {

if(grantResults[0] == PackageManager.PERMISSION_GRANTED) {

//TODO share 2 qq

}else{

goSetting();

}

}else{

super.onRequestPermissionsResult(requestCode, permissions, grantResults);

}

}

//进入app设置界面

private voidgoSetting() {

newMaterialDialog.Builder(mContext)

.title("温馨提示")

.content("分享需您同意读取内存卡权限")

.positiveText("去设置")

.negativeText("取消")

.onAny(newMaterialDialog.SingleButtonCallback() {

@Override

public voidonClick(@NonNullMaterialDialog dialog,@NonNullDialogAction which) {

if(which == DialogAction.NEGATIVE) {

dialog.dismiss();

}else if(which == DialogAction.POSITIVE) {

startAppSettings();

}

}

})

.show();

}

//启动应用的设置

private void startAppSettings() {

Intent intent =new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

intent.setData(Uri.parse(PACKAGE_URL_SCHEME+ getPackageName()));

startActivity(intent);

}

//静态变量

private static final String PACKAGE_URL_SCHEME="package:";//方案

/**

*权限状态码

*/

private static final int REQUEST_STORAGE_READ_ACCESS_PERMISSION=1024;

我复制的代码 空格没有进行处理

// dialog

compile'com.afollestad.material-dialogs:core:0.9.4.5'

上一篇下一篇

猜你喜欢

热点阅读