This is the first day of my participation in the More text Challenge. For details, see more text Challenge

Unity gets mobile permissions (storage, recording, camera, etc.) through Android Studio


Commonly used permissions

<! -- Access to the network, used to perform cloud voice capabilities --><uses-permission android:name="android.permission.INTERNET"/><! -- Obtain the mobile phone recorder use permission, dictation, recognition, semantic understanding need to use this permission --><uses-permission android:name="android.permission.RECORD_AUDIO"/><! -- Read the status of network information --><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><! -- Get current wifi status --><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/><! -- Allows an application to change the network connection state --><uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/><! -- Access to mobile phone information --><uses-permission android:name="android.permission.READ_PHONE_STATE"/><! -- Read contact permissions, you need to upload contact permissions --><uses-permission android:name="android.permission.READ_CONTACTS"/><! -- Write permission for external storage, which is required by the build syntax --><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><! -- read permission for external storage --><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/><! -- Configure permission to record application configuration information --><uses-permission android:name="android.permission.WRITE_SETTINGS"
    tools:ignore="ProtectedPermissions" /><! -- Mobile location information, used to provide location for semantic and other functions, to provide more accurate services --> <! - location information is sensitive information, by Setting. SetLocationEnable (falseClose the location request --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><! -- If you need to use face recognition, but also add: photo head permissions, take a photo needs to use --><uses-permission android:name="android.permission.CAMERA" />
Copy the code

1. Dynamic permission obtaining process

Android system from 6.0 on some dangerous permissions for dynamic access to the following figure is a dynamic access to the amount of flow chart

Two, the use of steps

There are roughly two ways to obtain permissions. One is to directly obtain the permissions required by the application when it is opened. In this case, all permissions are directly obtained during initialization. The other is to obtain permissions when a function is used. Such as in-app use of cameras, tape recorders, etc.

Next, use the second method to obtain permissions

1. Check whether you have obtained the permission

The code is as follows (example) :


int permissioncamera;
int permissionaudio;
// Determine whether the targetSdkVersion is greater than or equal to 23
if (MainActivity.this.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M) {
    // Android 6.0 or above
    permissioncamera = MainActivity.this.checkSelfPermission(android.Manifest.permission.CAMERA);
    permissionaudio = MainActivity.this.checkSelfPermission(android.Manifest.permission.RECORD_AUDIO);
} else {
    // Android 6.0 or below
    permissioncamera=PermissionChecker.checkSelfPermission(MainActivity.this,android.Manifest.permission.CAMERA);
    permissionaudio=PermissionChecker.checkSelfPermission(MainActivity.this,android.Manifest.permission.RECORD_AUDIO);
}
    // Check whether the camera permissions have been obtained:
    if(permissioncamera! = PackageManager.PERMISSION_GRANTED)// Check whether the recording permission has been obtained:
    if(permissionaudio! = PackageManager.PERMISSION_GRANTED)Copy the code

2. Check the current Android version

Because android 6.0 before and after the acquisition method is different, so in the use of the time to judge the version

The code is as follows (example) :

if (MainActivity.this.getApplicationInfo().targetSdkVersion >= Build.VERSION_CODES.M) 
Copy the code

3. Apply for dynamic permission

     MainPort.this.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 0x01);
     // Add a variety of permissions to this String
     
        @Override
        // This is the callback after the popover selection
    public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
        switch (requestCode) {
            // Allow permission
            case 0x01: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                } else {
                // Deny permission
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
                }
                // Refusing is not asking
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainPort.this,"You denied recording permission, please manually apply -> Settings, modify permissions.",Toast.LENGTH_LONG).show(); }});return; }}}Copy the code

We practice

AS the:

 public void permissionDemo(){
       
        // You need to obtain permission before using the recording
        int permissionCheck= MainPort.this.checkSelfPermission(android.Manifest.permission.RECORD_AUDIO);
        //int permissionCheck = ContextCompat.checkSelfPermission(MainPort.this, Manifest.permission.RECORD_AUDIO);
  
        / / if you have permission to return the PackageManager PERMISSION_GRANTED, otherwise returns PackageManager. PERMISSION_DENIED.
        if(permissionCheck! = PackageManager.PERMISSION_GRANTED) {// To obtain permission
            // Request permission
        
            MainPort.this.requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO}, 0x01);
            // The reason for using new String[] is that you can store multiple required permissions in String[] and request them at once
            / / will callback onRequestPermissionsResult () method
        }
    }
    @Override
    public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
        switch (requestCode) {
            case 0x01: {
                // If request is cancelled, the result arrays are empty.
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
     
                    // permission was granted, yay! Do the
                    // contacts-related task you need to do.
                } else {
                    // permission denied, boo! Disable the
                    // functionality that depends on this permission.
             
                }
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(MainPort.this,"You denied recording permission, please manually apply -> Settings, modify permissions.",Toast.LENGTH_LONG).show(); }});return; }}}Copy the code

The Unity end:

        AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
        AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");
        // Call this method where it needs to be used to get permission
        jo.Call("permissionDemo");
Copy the code

conclusion

This article describes how to get Access to Android in Unity, as well as an example!