/*** * 请求权限 *MainActivity */ private void requestPermission() { String[] permissionss = new String[]{ Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE }; if (!checkPermissionAllGranted(permissionss)) this.requestPermissions(permissionss, 10000); } /** * 检查是否拥有指定的所有权限 */ private boolean checkPermissionAllGranted(String[] permissions) { for (String permission : permissions) { if (this.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 只要有一个权限没有被授予, 则直接返回 false return false; } } return true; } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == 10000) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED && grantResults.length > 0) { Toast.makeText(MainActivity.this, "允许", Toast.LENGTH_LONG).show(); } else { Toast.makeText(MainActivity.this, "用户拒绝", Toast.LENGTH_LONG).show(); } } }
android 动态请求权限笔记
最新推荐文章于 2024-05-28 16:40:24 发布