if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. ActivityCompat.requestPermissions( // MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1); MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); return; }
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//安卓11文件权限 // 先判断有没有权限 if (Environment.isExternalStorageManager()) {//已有权限----------------- onHeadClick(); } else { Intent intent = new Intent(Settings.ACTION_MANAGE_APP_ALL_FILES_ACCESS_PERMISSION); intent.setData(Uri.parse("package:" + getPackageName())); startActivity(intent); return; } }
12------------------------------------------------------------------------------
final Intent enableIntent2 = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); startActivityForResult(enableIntent2, BLUE_REQUEST_CODE);
if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions( MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
rootPath = DocumentsUtils.getStoragePath(UserInfoActivity.this, false); //获取 可以插拔的 sd 卡 路径 LgqLogPlus.e("SD卡路径-=----- "+rootPath); if (DocumentsUtils.checkWritableRootPath(UserInfoActivity.this, rootPath)) { //检查sd卡路径是否有 权限 没有显示dialog String fileok12= SharedPreUtil.getString(mContext,"shierfileok","no"); if (fileok12.equals("ok")){ onHeadClick(); }else { if (android.os.Build.VERSION.SDK_INT >= 31) { showOpenDocumentTree(); }else { onHeadClick(); } } }else { onHeadClick(); }
String rootPath ; private void showOpenDocumentTree() { // String fileok12= SharedPreUtil.getString(mContext,"shierfileok","no"); // if (fileok12.equals("ok")){ // return; // } Intent intent = null; // if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) { if (android.os.Build.VERSION.SDK_INT >= 31) { StorageManager sm = getSystemService(StorageManager.class); StorageVolume volume = sm.getStorageVolume(new File(rootPath)); if (volume != null) { intent = volume.createAccessIntent(null); } } if (intent == null) { intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE); } startActivityForResult(intent, DocumentsUtils.OPEN_DOCUMENT_TREE_CODE); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Log.d("lgq","lgqrequ==== " + requestCode + ".....res===" + resultCode + "......" + data); switch (requestCode) { case DocumentsUtils.OPEN_DOCUMENT_TREE_CODE: if (resultCode==RESULT_OK){ // SharedPreUtil.putString("shierfileok","ok"); } if (data != null && data.getData() != null) { Uri uri = data.getData(); DocumentsUtils.saveTreeUri(this, rootPath, uri); } break; } }
if (ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return 1; }
1.需要用户手动赋予的权限( Dangerous Permissions)
所属权限组 | 权限 |
---|---|
日历 | READ_CALENDAR |
日历 | WRITE_CALENDAR |
相机 | CAMERA |
联系人 | READ_CONTACTS |
联系人 | WRITE_CONTACTS |
联系人 | GET_ACCOUNTS |
位置 | ACCESS_FINE_LOCATION |
位置 | ACCESS_COARSE_LOCATION |
麦克风 | RECORD_AUDIO |
电话 | READ_PHONE_STATE |
电话 | CALL_PHONE |
电话 | READ_CALL_LOG |
电话 | WRITE_CALL_LOG |
电话 | ADD_VOICEMAIL |
电话 | USE_SIP |
电话 | PROCESS_OUTGOING_CALLS |
传感器 | BODY_SENSORS |
短信 | SEND_SMS |
短信 | RECEIVE_SMS |
短信 | READ_SMS |
短信 | RECEIVE_WAP_PUSH |
短信 | RECEIVE_MMS |
存储 | READ_EXTERNAL_STORAGE |
存储 | WRITE_EXTERNAL_STORAGE |
还有指纹识别权限:
<uses-permission android:name="android.permission.USE_FINGERPRINT" />
注意:如果应用程序请求在AndroidManifest中列出的危险权限,并且应用程序已经在同一权限组中具有另一个危险权限,系统会立即授予权限,而不会与用户进行任何交互,。例如,如果一个应用程序先前已经请求并被授予READ_CONTACTS权限,然后它请求WRITE_CONTACTS(同属于联系人一组),系统会立即授予该权限,不会再弹出权限授予询问的对话框。
3、实现方法2——简便实现 RxPermissions 的使用
rxPermissions.requestEach(strPer) .subscribe(new Consumer<Permission>() { @Override public void accept(Permission permission) { if (permission.granted) { grantedCount++; if (grantedCount == strPer.length){ releaseCamera(); camera = Camera.open(1); camera.setDisplayOrientation(90); Log.e("lgq","同意000"); try { camera.setPreviewDisplay(surfaceView.getHolder()); camera.startPreview(); } catch (IOException e) { Log.e(TAG, "Error while trying to display the camera preview", e); } } } else if (permission.shouldShowRequestPermissionRationale) { // Denied permission without ask never again Toast.makeText(MainActivity.this, "Denied permission without ask never again", Toast.LENGTH_SHORT).show(); Log.e("lgq","拒绝111"); } else { // Denied permission with ask never again // Need to go to the settings Toast.makeText(MainActivity.this, "Permission denied, can't enable the camera", Toast.LENGTH_SHORT).show(); Log.e("lgq","取消1112222多次拒绝"); } } });
int grantedLocationCount = 0; private void checkPermission() { final String strPer[] = {Manifest.permission.WRITE_EXTERNAL_STORAGE , Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION }; grantedLocationCount = 0; final RxPermissions rxPermissions = new RxPermissions(this); rxPermissions.requestEach(strPer) .subscribe(new Consumer<Permission>() { @Override public void accept(Permission permission) { if (permission.granted) { grantedLocationCount++; if (grantedLocationCount == strPer.length) { //请求成功操作 isWifi5G(); } } else if (permission.shouldShowRequestPermissionRationale) { // ShowPermissionDialog(LangComm.getString("need_permissions")); } else { // ShowPermissionDialog(LangComm.getString("android_check_permissions")); } } }); }
demo连接:https://download.csdn.net/download/meixi_android/86519919
android.permission.WRITE_SETTINGS 申请
Context context = getApplicationContext(); // Check whether has the write settings permission or not. boolean settingsCanWrite = Settings.System.canWrite(context); if(!settingsCanWrite) { // If do not have write settings permission then open the Can modify system settings panel. Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS); startActivity(intent); }else { // If has permission then show an alert dialog with message. AlertDialog alertDialog = new AlertDialog.Builder(LinyuMainFragmentActy.this).create(); alertDialog.setMessage("You have system write settings permission now."); alertDialog.show(); }