Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- ...............................
- ...............................
- // Handle run time permission
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- if (getAllPermission()) {
- proceedAfterPermission();
- }
- } else
- proceedAfterPermission();
- }
- private boolean getAllPermission() {
- int readPhoneState = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_PHONE_STATE);
- int readExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE);
- int writeExternalStorage = ContextCompat.checkSelfPermission(mContext, Manifest.permission.WRITE_EXTERNAL_STORAGE);
- List<String> listPermissionsNeeded = new ArrayList<>();
- if (readPhoneState != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.READ_PHONE_STATE);
- }
- if (readExternalStorage != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.READ_EXTERNAL_STORAGE);
- }
- if (writeExternalStorage != PackageManager.PERMISSION_GRANTED) {
- listPermissionsNeeded.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
- }
- if (!listPermissionsNeeded.isEmpty()) {
- if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
- requestPermissions(listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
- return false;
- }
- }
- return true;
- }
- @Override
- public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
- if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) {
- //check if all permissions are granted
- boolean allgranted = false;
- for (int grantResult : grantResults) {
- if (grantResult == PackageManager.PERMISSION_GRANTED) {
- allgranted = true;
- } else {
- allgranted = false;
- break;
- }
- }
- if (allgranted) {
- proceedAfterPermission();
- } else {
- AlertDialog.Builder builder = new AlertDialog.Builder(SignInActivity.this);
- builder.setTitle("Need Multiple Permissions");
- builder.setMessage("This app needs Phone and Storage permissions.");
- builder.setPositiveButton("Grant", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- getAllPermission();
- }
- });
- builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.cancel();
- finish();
- }
- });
- builder.show();
- }
- }
- }
- @SuppressLint({"MissingPermission", "HardwareIds"})
- private void proceedAfterPermission() {
- //Do your suff...
- TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
- assert mTelephonyManager != null;
- deviceImei = mTelephonyManager.getDeviceId();
- Log.e(TAG, "IMEI: " + deviceImei);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement