android敏感权限库_Android 敏感权限申请

本文介绍如何在Android应用中处理运行时权限,特别是在Android M及以上版本。通过使用EasyPermissions库,请求和管理文件读写权限,并处理用户拒绝权限的情况,包括提示用户在应用设置中开启被拒绝的权限。
摘要由CSDN通过智能技术生成

packagecom.example.testperms;importandroid.Manifest;importandroid.content.DialogInterface;importandroid.content.Intent;importandroid.net.Uri;importandroid.os.Build;importandroid.provider.Settings;importandroid.support.annotation.NonNull;importandroid.support.v7.app.AlertDialog;importandroid.support.v7.app.AppCompatActivity;importandroid.os.Bundle;importandroid.util.Log;importjava.util.List;importpub.devrel.easypermissions.AfterPermissionGranted;importpub.devrel.easypermissions.EasyPermissions;importpub.devrel.easypermissions.PermissionRequest;public class MainActivity extends AppCompatActivity implementsEasyPermissions.PermissionCallbacks {private String TAG = "MainActivity";private static final int REQUEST_CODE_RUNTIME_PERMS = 0;boolean isDenied = false;

@Overrideprotected voidonCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

methodRequiresTwoPermission();

}

@AfterPermissionGranted(REQUEST_CODE_RUNTIME_PERMS)private voidmethodRequiresTwoPermission() {if (Build.VERSION.SDK_INT >=Build.VERSION_CODES.M) {

String[] perms={Manifest.permission.READ_EXTERNAL_STORAGE,

Manifest.permission.WRITE_EXTERNAL_STORAGE};if (EasyPermissions.hasPermissions(this, perms)) {//Already have permission, do the thing//...

} else{//Do not have permissions, request them now

EasyPermissions.requestPermissions(this, "文件读写权限",

REQUEST_CODE_RUNTIME_PERMS, perms);//EasyPermissions.requestPermissions(//new PermissionRequest.Builder(this, REQUEST_CODE_RUNTIME_PERMS, perms)//.setRationale(R.string.str_rw_perms)//.setPositiveButtonText(R.string.rationale_ask_ok)//.setNegativeButtonText(R.string.rationale_ask_cancel).setTheme(R.style.Theme_AppCompat)//.build());

}

}

}

@Overridepublic void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {super.onRequestPermissionsResult(requestCode, permissions, grantResults);//Forward results to EasyPermissions

EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);if (requestCode ==REQUEST_CODE_RUNTIME_PERMS) {//成功获取运行时授权

} else{//获取运行时权限失败

}

}

@Overridepublic void onPermissionsGranted(int requestCode, Listperms) {

Log.d(TAG,"onPermissionsGranted:" + requestCode + ":" +perms.size());

}

@Overridepublic void onPermissionsDenied(int requestCode, Listperms) {//methodRequiresTwoPermission();

Log.d(TAG, "onPermissionsDenied:" + requestCode + ":" +perms.size());//(Optional) Check whether the user denied any permissions and checked "NEVER ASK AGAIN."//This will display a dialog directing them to enable the permission in app settings.

if (EasyPermissions.somePermissionPermanentlyDenied(this, perms)) {new AlertDialog.Builder(this)

.setTitle("权限提示")

.setMessage("本项认证需要外置存储权限,点击确认后在新页面滑动到最下方,在权限管理处,开启外置存储权限")

.setNegativeButton("取消", newDialogInterface.OnClickListener() {

@Overridepublic void onClick(DialogInterface dialog, intwhich) {

finish();

}

})

.setPositiveButton("确认", newDialogInterface.OnClickListener() {

@Overridepublic void onClick(DialogInterface dialog, intwhich) {//跳转到APP设置页面

Intent intent = newIntent();

intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);

Uri uri= Uri.fromParts("package", getPackageName(), null);

intent.setData(uri);

startActivity(intent);

}

})

.create().show();

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值