@Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { System.out.println("77"+requestCode); System.out.println(grantResults); switch (requestCode) { case MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE: System.out.println("88"+PackageManager.PERMISSION_GRANTED); if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { // do your stuff System.out.println("99999"); } else { Toast.makeText(IndexActivity.this, "GET_ACCOUNTS Denied", Toast.LENGTH_SHORT).show(); } break; default: System.out.println("101010"); super.onRequestPermissionsResult(requestCode, permissions, grantResults); } } public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123; public boolean checkPermissionREAD_EXTERNAL_STORAGE( final Context context) { System.out.println("now----------------"); int currentAPIVersion = Build.VERSION.SDK_INT; System.out.println("now++++"+currentAPIVersion); System.out.println("now+"+android.os.Build.VERSION_CODES.M); if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) { System.out.println("no000000*****0"+Manifest.permission.READ_EXTERNAL_STORAGE); System.out.println("no000000*****1"+ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE)); System.out.println("no00000022222222"+PackageManager.PERMISSION_GRANTED); if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { System.out.println("111111111111"); if (ActivityCompat.shouldShowRequestPermissionRationale( (Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) { System.out.println("5555555"); showDialog("External storage", context, Manifest.permission.READ_EXTERNAL_STORAGE); } else { System.out.println("666666666666"); ActivityCompat .requestPermissions( (Activity) context, new String[] { Manifest.permission.READ_EXTERNAL_STORAGE }, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } return false; } else { return true; } } else { return true; } } public void showDialog(final String msg, final Context context, final String permission) { AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context); alertBuilder.setCancelable(true); alertBuilder.setTitle("Permission necessary"); alertBuilder.setMessage(msg + " permission is necessary"); alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { ActivityCompat.requestPermissions((Activity) context, new String[] { permission }, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE); } }); AlertDialog alert = alertBuilder.create(); alert.show(); }
android 原生开发 动态获取权限问题
最新推荐文章于 2023-11-27 13:52:56 发布