我用 ndk 构建了一些命令行工具并在/data/local/tmp 中执行它。现在,当我在命令行工具中调用某些 OpenSLES API 时,它会提示我“需要 android.permission.RECORD_AUDIO”:
W/AudioRecord( 4226): AUDIO_INPUT_FLAG_FAST denied by client
W/ServiceManager( 207): Permission failure: android.permission.RECORD_AUDIO from uid=2000 pid=4226
E/ ( 207): Request requires android.permission.RECORD_AUDIO
D/PowerManagerService( 964): handleSandman: canDream=false, mWakefulness=Asleep
E/AudioFlinger( 207): openRecord() permission denied: recording not allowed
E/AudioRecord( 4226): AudioFlinger could not create record track, status: -1
E/libOpenSLES( 4226): android_audioRecorder_realize(0x453430) error creating AudioRecord object
W/libOpenSLES( 4226): Leaving Object::Realize (SL_RESULT_CONTENT_UNSUPPORTED)
我还尝试通过 pm grant 授予 shell:
pm grant "com.android.shell" android.permission.RECORD_AUDIO
pm grant "com.android.shell" android.permission.RECORD_AUDIO
pm grant "com.android.shell" android.permission.RECORD_AUDIO
Operation not allowed: java.lang.SecurityException: Package com.android.shell has not requested permission android.permission.RECORD_AUDIO
更改/system/etc/permissions/platform.xml 也没有效果。
我可以只在 android shell 中调试我的 OpenSLES 演示吗?我怎样才能在 shell 中获得更多权限。
我是否必须为每个实验代码 fragment 创建一个 jni 和 java 项目,并在我更改某些 C++ 接口(interface)时一起修改它们?
我可以直接在命令工具中从 shell 访问 RECORD_AUDIO、CAMERA 吗?
最佳答案
这是 new permissions model in Android Marshmallow在行动中。要获得此权限,您需要提示用户授予它。这是我的做法:
每当我需要 RECORD_AUDIO 许可时,我都会运行检查以查看我是否拥有它:
private boolean hasRecordAudioPermission(){ boolean hasPermission = (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED); log("Has RECORD_AUDIO permission? " + hasPermission); return hasPermission; }
如果我没有,那么请求它
private void requestRecordAudioPermission(){ String requiredPermission = Manifest.permission.RECORD_AUDIO; // If the user previously denied this permission then show a message explaining why // this permission is needed if (ActivityCompat.shouldShowRequestPermissionRationale(this, requiredPermission)) { showToast("This app needs to record audio through the microphone...."); } // request the permission. ActivityCompat.requestPermissions(this, new String[]{requiredPermission}, PERMISSIONS_REQUEST_RECORD_AUDIO); } @Override public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { // This method is called when the user responds to the permissions dialog }
关于android - 如何授予android shell 用户更多权限?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32217831/