Android permission request (plural)

Android Runtime Permission Request

A long time ago, I wrote how to allow individual permissions from the setting screen in Android system permissions, but the app displays a dialog. I left it for a long time trying to find out how. I checked it out, so I will write it again.

This Permission

This time, we will use the following Permission to use the recording application created in the past.

AndroidManifest.xml


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.VIBRATE"/>

code

The flow is that the Permission dialog is displayed and the permission is displayed in the MainActivity, and when it is completed, the transition to the startRecordingActivity is performed and recording is started.

MainActivity.java


package com.example.testrecordingpermission;

import android.content.Intent;
import android.content.pm.PackageManager;
import android.Manifest;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private final int REQUEST_CODE = 1000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Android 6,Check permissions with API 23 and above
        if(Build.VERSION.SDK_INT >= 23) {
            String[] permissions = {
                    Manifest.permission.WRITE_EXTERNAL_STORAGE,
                    Manifest.permission.RECORD_AUDIO,
                    Manifest.permission.VIBRATE
            };
            checkPermission(permissions, REQUEST_CODE);
        }

        startRecordingActivity();
    }

    //Confirmation of location information permission
    public void checkPermission(final String[] permissions,final int request_code){
        //Only those that are not allowed will be displayed in the dialog
        ActivityCompat.requestPermissions(this, permissions, request_code);
    }

    //requestPermissions callback
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch(requestCode) {

            case REQUEST_CODE:
                for(int i = 0; i < permissions.length; i++ ){
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {
                        Toast toast = Toast.makeText(this,
                                "Added Permission: " + permissions[i], Toast.LENGTH_SHORT);
                        toast.show();
                    } else {
                        Toast toast = Toast.makeText(this,
                                "Rejected Permission: " + permissions[i], Toast.LENGTH_SHORT);
                        toast.show();
                    }
                }
                break;
            default:
                break;
        }
    }

    //StartRecording with Intent
    private void startRecordingActivity() {
        Intent intent = new Intent(getApplication(), RecordingActivity.class);
        startActivity(intent);
    }
}

result

VIBRATE Permission does not require permission, so the WRITE_EXTERNAL_STRAGE and RECORD_AUDIO dialogs are displayed. success.

      

The dialog display / permission was successful, but it was not displayed when the app was started, and it was displayed only after the app was closed. I really want it to be displayed immediately after launching the app. Check again.

Recommended Posts

Android permission request (plural)
Android memo-I was addicted to Permission