简体   繁体   中英

How to grant android.permission.RECORD_AUDIO permissions to android shell user?

I build some command line tool with android NDK and execute it in /data/local/tmp. Now it prompts me “requires android.permission.RECORD_AUDIO”. It's usually in AndroidManifest.xml for java application, but how to grant it to android shell user?

Source code like:

sp<AudioRecord> rec = new AudioRecord(AUDIO_SOURCE_MIC, 44100, AUDIO_FORMAT_PCM_16_BIT, AUDIO_CHANNEL_IN_MONO);

Logcat:

I/AudioFlinger( 3772): AudioFlinger's thread 0xf0e838c0 ready to run
W/ServiceManager( 3772): Permission failure:android.permission.RECORD_AUDIO from uid=2000 pid=-1
E/        ( 3772): Request requires android.permission.RECORD_AUDIO
E/AudioFlinger( 3772): openRecord() permission denied: recording not allowed
E/AudioRecord(14132): AudioFlinger could not create record track, status: -1
--------- beginning of crash
...

After Lollipop, Android platform is using real time permissions. If your app running in Marshmallow and after versions, you should grant your permissions in Activity.

Before request permissions:

 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
                PackageManager.PERMISSION_GRANTED) {
            // put your code for Version>=Marshmallow
        } else {
            if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
                Toast.makeText(this,
                        "App required access to audio", Toast.LENGTH_SHORT).show();
            }
            requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
            }, REQUEST_CAMERA_PERMISSION_RESULT);
        }

    } else {
        // put your code for Version < Marshmallow
    }

After this, overraide this method and put your code:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

  if (requestCode == REQUEST_AUDIO_PERMISSION_RESULT) {
        if (grantResults[0] != PackageManager.PERMISSION_GRANTED) {
            Toast.makeText(getApplicationContext(),
                    "Application will not have audio on record", Toast.LENGTH_SHORT).show();
        }
    }
}
  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 (getActivity().checkCallingOrSelfPermission(requiredPermission) == PackageManager.PERMISSION_GRANTED) {

        } else {

            Toast.makeText(getActivity(), "This app needs to record audio through the microphone....", Toast.LENGTH_SHORT).show();
            requestPermissions(new String[]{requiredPermission}, 101);
        }


    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
                                           String permissions[], int[] grantResults) {
        if (requestCode == 101 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            // This method is called when the  permissions are given
        }

    }

Execute this code if you have device with marshmallow or upper OS

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM