您好我遇到的问题是某些设备没有显示运行时权限。问题是什么? 我面临着一些设备和应用程序崩溃。但是当我进入设置并允许权限时,它很好
答案 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%确定。