如何给Android的shell用户授予更多权限?

3

我使用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)

我也尝试授予Shell权限,使用pm grant命令:
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中获得更多权限。

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

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


@Samveen 你有一篇关于在安卓终端运行C++应用程序的帖子。你知道如何给C++应用程序授予权限吗? - skywind3000
是的,您可以以超级用户身份运行它。您需要一个已经取得了 root 权限的设备。 - Alex Cohn
1个回答

5
这是Android Marshmallow中的新权限模型。要获取此权限,您需要提示用户授权。以下是我的操作步骤:
  1. Whenever I need RECORD_AUDIO permission I run a check to see if I have it:

    private boolean hasRecordAudioPermission(){
        boolean hasPermission = (ContextCompat.checkSelfPermission(this,
            Manifest.permission.RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED);
    
        log("Has RECORD_AUDIO permission? " + hasPermission);
        return hasPermission;
    }
    
  2. If I don't have it then request it

    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
    }
    

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接