如何向android shell用户授予更多权限?

sky*_*000 3 shell android android-ndk opensl

我用 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)
Run Code Online (Sandbox Code Playgroud)

我还尝试使用 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
Run Code Online (Sandbox Code Playgroud)

更改 /system/etc/permissions/platform.xml 也没有效果。

我可以在 android shell 中调试我的 OpenSLES 演示吗?我怎样才能在 shell 中获得更多的许可。

我是否必须为每个实验代码片段创建一个 jni 和 java 项目,并在更改某些 C++ 接口时一起修改它们?

我可以直接在 shell 的命令工具中访问 RECORD_AUDIO、CAMERA 吗?

don*_*ner 5

这是Android Marshmallow中的新权限模型。要获得此权限,您需要提示用户授予它。这是我所做的:

  1. 每当我需要 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;
    }
    
    Run Code Online (Sandbox Code Playgroud)
  2. 如果我没有,那么请求它

    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
    }
    
    Run Code Online (Sandbox Code Playgroud)