某些具有棉花糖的设备上没有显示运行时权限对话框

时间:2016-12-07 06:38:04

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

您好我遇到的问题是某些设备没有显示运行时权限。问题是什么? 我面临着一些设备和应用程序崩溃。但是当我进入设置并允许权限时,它很好

3 个答案:

答案 0 :(得分:0)

处理运行时权限的最佳做法是通过 RxPermission

尝试 RxPermission 将解决您的问题

答案 1 :(得分:0)

String[] permissions = new String[]{
        Manifest.permission.WRITE_SETTINGS,
        Manifest.permission.SYSTEM_ALERT_WINDOW,
        Manifest.permission.WAKE_LOCK,
        Manifest.permission.INTERNET,
        Manifest.permission.READ_PHONE_STATE,
        Manifest.permission.READ_EXTERNAL_STORAGE,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.BROADCAST_STICKY,
        Manifest.permission.VIBRATE,
        Manifest.permission.RECORD_AUDIO,

};
 private boolean checkPermissions() {
    int result;
    List<String> listPermissionsNeeded = new ArrayList<>();
    for (String p : permissions) {
        result = ContextCompat.checkSelfPermission(getActivity(), p);
        if (result != PackageManager.PERMISSION_GRANTED) {
            listPermissionsNeeded.add(p);
        }
    }
    if (!listPermissionsNeeded.isEmpty()) {
        ActivityCompat.requestPermissions(root, listPermissionsNeeded.toArray(new String[listPermissionsNeeded.size()]), REQUEST_ID_MULTIPLE_PERMISSIONS);
        return false;
    }
    return true;
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    if (requestCode == REQUEST_ID_MULTIPLE_PERMISSIONS) {
        if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // selectImage();
            // isPermission = true;
        }
        return;
    }
}

答案 2 :(得分:0)

您可以在应用程序首次启动时以编程方式设置“不带对话框的权限”。

private void setPermission(string permission){
        Process process = null;
        try {
            process = Runtime.getRuntime().exec("/system/bin/su -c pm grant " + [your package name] + " " + permission);
            process.waitFor();
            Log.d(TAG, "permission exec done " + process.exitValue());

        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}

现在你可以用:

来调用它
setPermission("android.permission.SYSTEM_ALERT_WINDOW");

如果你需要sudo,我不是100%确定。