Reputation: 401
I heard that miui phone also ask for permission as marshmallow so I created some code for it.
It works well on other phones but when I tested it on MIUI devices it returns 0 wether I accept permission or denied permission.
Below I put my code which check permission
if ( android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M )
{
checkAndRequestPermissions(); // this method first check for permisison if not granted then call ActivityCompact.checkSelfPermisison(context,permisison);
}
else
{
int permission = PermissionChecker.checkSelfPermission(MainActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
Log.d("Permission status","Status"+permission);
}
Upvotes: 3
Views: 748
Reputation: 299
please add this code and call checkUsagePermission function for checking read sms permission in miui above marshmallow
private boolean checkUsagePermission() {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
AppOpsManager appOps = (AppOpsManager) getSystemService(Context.APP_OPS_SERVICE);
int mode = 0;
mode = appOps.checkOpNoThrow("android:read_sms", android.os.Process.myUid(), getPackageName());
boolean granted = mode == AppOpsManager.MODE_ALLOWED;
if (!granted) {
//write your code for accept that permission
return false;
}
}
return true;
}
Upvotes: 3
Reputation: 207
this is the sample code for marshmallow and above versions:
public static class Utility { public static final int MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE = 123;
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public static boolean checkPermission(final Context context) {
int currentAPIVersion = Build.VERSION.SDK_INT;
if (currentAPIVersion >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale((Activity) context, Manifest.permission.READ_EXTERNAL_STORAGE)) {
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(context, R.style.MyAlertDialogStyle);
alertBuilder.setCancelable(true);
alertBuilder.setTitle("Permission necessary");
alertBuilder.setMessage("External storage permission is necessary");
alertBuilder.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
public void onClick(DialogInterface dialog, int which) {
ActivityCompat.requestPermissions((Activity) context, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_EXTERNAL_STORAGE);
}
});
AlertDialog alert = alertBuilder.create();
alert.show();
} else {
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;
}
}
}
this is a method that checks for permission for reading external storage. please refer the code and manipulate accordingly. Hope this helps you out.
also add this code in onCreate method
boolean result = Utility.checkPermission(MainActivity.this);
Upvotes: 0