如何检查miui手机中的短信权限

时间:2016-12-29 06:22:34

标签: android android-6.0-marshmallow android-permissions user-permissions runtime-permissions

我听说miui手机也要求获得棉花糖的许可,所以我为它创建了一些代码。

它在其他手机上运行良好,但当我在MIUI设备上测试时,返回0,我接受许可或拒绝许可。

下面我提交了检查权限的代码

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);
}

2 个答案:

答案 0 :(得分:2)

请添加此代码并调用checkUsagePermission函数以检查marshmalowlow以上miui的读取短信权限

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;
}

答案 1 :(得分:0)

这是marshmallow及以上版本的示例代码:

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;
        }
    }
}

这是一种检查读取外部存储的权限的方法。请参考代码并进行相应的操作。希望这会帮助你。

还在onCreate方法

中添加此代码

boolean result = Utility.checkPermission(MainActivity.this);