android动态权限申请
程序员文章站
2022-08-12 15:47:26
如代码所示:public class MainActivity extends AppCompatActivity { private String[] permissionList = new String[]{ //申请的权限列表 Manifest.permission.READ_CONTACTS, Manifest.permission.READ_EXTERNAL_STORAGE }; @Override...
如代码所示:
public class MainActivity extends AppCompatActivity {
private String[] permissionList = new String[]{ //申请的权限列表
Manifest.permission.READ_CONTACTS,
Manifest.permission.READ_EXTERNAL_STORAGE
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addPermissByPermissionList(this,permissionList,1);
}
/**
* 动态权限
*/
public void addPermissByPermissionList(Activity activity, String[] permissions, int request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //Android 6.0开始的动态权限,这里进行版本判断
ArrayList<String> mPermissionList = new ArrayList<>();
for (int i = 0; i < permissions.length; i++) {
if (ContextCompat.checkSelfPermission(activity, permissions[i])
!= PackageManager.PERMISSION_GRANTED) {
mPermissionList.add(permissions[i]);
}
}
if (mPermissionList.isEmpty()) { //非初次进入App且已授权
// showContacts();
Toast.makeText(this, "已授权", Toast.LENGTH_SHORT).show();
} else {
//请求权限方法
String[] permissionsNew = mPermissionList.toArray(new String[mPermissionList.size()]);//将List转为数组
ActivityCompat.requestPermissions(activity, permissionsNew, request); //这个触发下面onRequestPermissionsResult这个回调
}
}
}
/**
* requestPermissions的回调
* 一个或多个权限请求结果回调
*/
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
boolean hasAllGranted = true;
//判断是否拒绝 拒绝后要怎么处理 以及取消再次提示的处理
if (requestCode == 1) {
for (int grantResult : grantResults) {
//grantResult 对应申请权限数组的单个权限是否申请回来的结果
if (grantResult == PackageManager.PERMISSION_GRANTED) {
hasAllGranted = false;
break;
}
}
if (hasAllGranted) { //同意权限做的处理,开启服务提交通讯录
// showContacts();
Toast.makeText(this, "同意授权", Toast.LENGTH_SHORT).show();
} else { //拒绝授权做的处理,弹出弹框提示用户授权
dealwithPermiss(MainActivity.this, permissions[0]);
Toast.makeText(this, "不同意授权", Toast.LENGTH_SHORT).show();
}
}
}
//未授权申请权限接下来的操作
public void dealwithPermiss(final Activity context, String permission) {
if (!ActivityCompat.shouldShowRequestPermissionRationale(context, permission)) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("操作提示")
.setMessage("注意:当前缺少必要权限!\n请点击“设置”-“权限”-打开所需权限\n最后点击两次后退按钮,即可返回")
.setPositiveButton("去授权", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", context.getApplicationContext().getPackageName(), null);
intent.setData(uri);
context.startActivity(intent);
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "取消操作", Toast.LENGTH_SHORT).show();
}
}).show();
}
}
}
本文地址:https://blog.csdn.net/TianciZhu/article/details/107929514
上一篇: axios在vue中的封装,可直接复用
下一篇: Orabbix监控Oracle 11g