unity + android permissions - Open Application does not play the permission request permission dynamic

Because before the game I need to share pictures, will request external storage, first open the game there will pop: "Allow this device to access photos and videos you"

Many people are very sensitive to this, afraid you access to their photos privacy, refused permission to see this, even unloaded, in fact, we just want to share screenshots game content to other players, but could not rely on other people do not believe ah.

If we really want or need this permission to share pictures, then we can not let it pop up at startup, but when the pop-up click share permissions (dynamic requests permission)

First, shielded permissions pop, AndroidManifest.xml which joined

<-! Unity packaged pop shield android permissions -> 
<android Meta-Data: name = " unityplayer.SkipPermissionsDialog " android: value = " to true " />

Next is requesting permission to request the position you need

unity end

// AndroidJavaClass in UnityEngine inside the namespace, a using UnityEngine;
#if
UNITY_ANDROID
AndroidJavaClass androidClass
= new new AndroidJavaClass ( " com.xxx.xxx.UnityPlayerActivity " ); // get class, fill out the right of Activity
androidClass . for CallStatic ( " requestExternalStorage " ); // call the static method, requestExternalStorage this function names you can take your own, remember that it is a static function
#endif

java end, written on the inside of your Activity

Import android.support.v4.content.PermissionChecker;
 Import android.support.v4.app.ActivityCompat;
 // As mentioned static methods, android.permission.WRITE_EXTERNAL_STORAGE is an external storage permissions, other permissions empathy can also be dynamically request 
public  static  void requestExternalStorage () {
     // check the permissions to avoid duplication of the same request permission parameters: Activity, authority name 
        IF (PermissionChecker.checkSelfPermission ( the this !, "android.permission.WRITE_EXTERNAL_STORAGE") = 0 ) { 
            ActivityCompat.requestPermissions ( the this , new new String [] { "android.permission.WRITE_EXTERNAL_STORAGE"}, 100 ); // request permission parameters: activity, authority name, request code (different permission requirements of different request code, may set their own, such as my authority 100 the other can fill 102, 103 ...)
        }
}

This prevents application of a permission to open a pop dynamic request permission at corresponding locations

Guess you like

Origin www.cnblogs.com/xianguoguo/p/11105819.html