Advertisement
chayanforyou

Android Runtime Permission

Feb 15th, 2019
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.74 KB | None | 0 0
  1.     @Override
  2.     protected void onCreate(Bundle savedInstanceState) {
  3.         super.onCreate(savedInstanceState);
  4.         ...............................
  5.         ...............................
  6.  
  7.         // Handle run time permission
  8.         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  9.             if (getAllPermission()) {
  10.                 proceedAfterPermission();
  11.             }
  12.         } else
  13.             proceedAfterPermission();
  14.     }
  15.  
  16.  
  17.     private boolean getAllPermission() {
  18.  
  19.         int readPhoneState = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE);
  20.         int readExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
  21.         int writeExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
  22.  
  23.  
  24.         List<String> listPermissionsNeeded = new ArrayList<>();
  25.  
  26.         if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
  27.             listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
  28.         }
  29.  
  30.         if (readExternalStorage != PackageManager.PERMISSION_GRANTED) {
  31.             listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
  32.         }
  33.  
  34.         if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {
  35.             listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
  36.         }
  37.  
  38.         if (!listPermissionsNeeded.isEmpty()) {
  39.             if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
  40.                 requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
  41.                 return false;
  42.             }
  43.         }
  44.         return true;
  45.     }
  46.  
  47.     @Override
  48.     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
  49.         if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) {
  50.             //check if all permissions are granted
  51.             boolean allgranted = false;
  52.             for (int grantResult : grantResults) {
  53.                 if (grantResult == PackageManager.PERMISSION_GRANTED) {
  54.                     allgranted = true;
  55.                 } else {
  56.                     allgranted = false;
  57.                     break;
  58.                 }
  59.             }
  60.  
  61.             if (allgranted) {
  62.                 proceedAfterPermission();
  63.             } else {
  64.                 AlertDialog.Builder builder = new AlertDialog.Builder(SignInActivity.this);
  65.                 builder.setTitle("Need Multiple Permissions");
  66.                 builder.setMessage("This app needs Phone and Storage permissions.");
  67.                 builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
  68.                     @Override
  69.                     public void onClick(DialogInterface dialog, int which) {
  70.                         dialog.cancel();
  71.                         getAllPermission();
  72.                     }
  73.                 });
  74.                 builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  75.                     @Override
  76.                     public void onClick(DialogInterface dialog, int which) {
  77.                         dialog.cancel();
  78.                         finish();
  79.                     }
  80.                 });
  81.                 builder.show();
  82.             }
  83.         }
  84.     }
  85.  
  86.     @SuppressLint({"MissingPermission", "HardwareIds"})
  87.     private void proceedAfterPermission() {
  88.  
  89.         //Do your suff...
  90.         TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
  91.         assert mTelephonyManager != null;
  92.         deviceImei = mTelephonyManager.getDeviceId();
  93.         Log.e(TAG, "IMEI: " + deviceImei);
  94.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement