2016-04-01 96 views
0

您好我目前正在嘗試構建Unity3D中所謂的android插件:NoodlePermissionGranter,以便我可以在運行時請求危險的權限。創建一個android unity3d插件Marshmallow 6.0危險權限

我在stackoverflow的另一個問題上找到了插件。從題目中使用

答: Implementing Android 6.0 permissions in unity3d]

我做了一些改動,使其更容易建立,但預期它不工作。

如果插件尚未被授予,應該請求危險權限。 當試圖請求權限時,它立即回調PERMISSION_DENIED。

提前抱歉我的英語不好。

編輯:沒有錯誤。

NoodlePermissionGranter.Java

package com.noodlecake.unityplugins; 


     /////////////////////////////////////////////////////////// 
    ///////////////// NoodlePermissionGranter ///////////////// 
    /// Implements runtime granting of Android permissions. /// 
    /// This is necessary for Android M (6.0) and above. ////// 
    /////////////////////////////////////////////////////////// 
//////////////////// Noodlecake Studios /////////////////// 
/////////////////////////////////////////////////////////// 

import android.Manifest; 
import android.os.Build; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.util.Log; 
import android.content.pm.PackageManager; 
import com.unity3d.player.UnityPlayer; 

public class NoodlePermissionGranter 
{ 
    // Only implements WRITE_EXTERNAL_STORAGE so far. 
    // Implement the rest by matching the enum in NoodlePermissionGranter.cs 
    // to the getPermissionStringFromEnumInt below. 

    private final static String UNITY_CALLBACK_GAMEOBJECT_NAME = "NoodlePermissionGranter"; 
    private final static String UNITY_CALLBACK_METHOD_NAME = "permissionRequestCallbackInternal"; 
    private final static String PERMISSION_GRANTED = "PERMISSION_GRANTED"; // this will be an arg to the above method 
    private final static String PERMISSION_DENIED = "PERMISSION_DENIED"; 

    public static String getPermissionStringFromEnumInt(int permissionEnum) throws Exception 
    { 
     switch (permissionEnum) 
     { 
      case 0: 
       return Manifest.permission.WRITE_EXTERNAL_STORAGE; 
      case 1: 
       return Manifest.permission.READ_PHONE_STATE; 
      case 2: 
       return Manifest.permission.READ_EXTERNAL_STORAGE; 
      // "and the rest is still unwritten" - Natasha Bedingfield 
     } 
     Log.e("NoodlePermissionGranter", "Error. Unknown permissionEnum " + permissionEnum); 
     throw new Exception(String.format("Error. Unknown permissionEnum %d",permissionEnum)); 
    } 

    public static void grantPermission(Activity currentActivity, int permissionEnum) 
    { 
     // permission enum must match ordering in NoodlePermissionGranter.cs 
     final Activity act = currentActivity; 
     Log.i("NoodlePermissionGranter","grantPermission " + permissionEnum) ; 
     if (Build.VERSION.SDK_INT < 23) { 
      Log.i("NoodlePermissionGranter","Build.VERSION.SDK_INT < 23 (" + Build.VERSION.SDK_INT+")"); 
      return; 
     } 

     try 
     { 
      final int PERMISSIONS_REQUEST_CODE = permissionEnum; 
      final String permissionFromEnumInt = getPermissionStringFromEnumInt(permissionEnum); 
      if (currentActivity.checkCallingOrSelfPermission(permissionFromEnumInt) == PackageManager.PERMISSION_GRANTED) { 
       Log.i("NoodlePermissionGranter", "already granted"); 
       UnityPlayer.UnitySendMessage(UNITY_CALLBACK_GAMEOBJECT_NAME, UNITY_CALLBACK_METHOD_NAME, PERMISSION_GRANTED); 
       return; 
      } 

      final FragmentManager fragmentManager = currentActivity.getFragmentManager(); 
      final Fragment request = new Fragment() { 

       @Override public void onStart() 
       { 
        super.onStart(); 
        Log.i("NoodlePermissionGranter","fragment start"); 
        String[] permissionsToRequest = new String [] {permissionFromEnumInt}; 
        Log.i("NoodlePermissionGranter","fragment start " + permissionsToRequest[0]); 
        requestPermissions(permissionsToRequest, PERMISSIONS_REQUEST_CODE); 
       } 

       @Override public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
       { 
        Log.i("NoodlePermissionGranter", "onRequestPermissionsResult"); 
        if (requestCode != PERMISSIONS_REQUEST_CODE) 
         return; 

        if (grantResults.length > 0 
         && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 
         // permission was granted, yay! Do the 
         // contacts-related task you need to do. 
         Log.i("NoodlePermissionGranter", PERMISSION_GRANTED); 
         UnityPlayer.UnitySendMessage(UNITY_CALLBACK_GAMEOBJECT_NAME, UNITY_CALLBACK_METHOD_NAME, PERMISSION_GRANTED); 
        } else { 

         // permission denied, boo! Disable the 
         // functionality that depends on this permission. 
         Log.i("NoodlePermissionGranter",PERMISSION_DENIED); 
         UnityPlayer.UnitySendMessage(UNITY_CALLBACK_GAMEOBJECT_NAME, UNITY_CALLBACK_METHOD_NAME, PERMISSION_DENIED); 
        } 


        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
        fragmentTransaction.remove(this); 
        fragmentTransaction.commit(); 

        // shouldBeOkayToStartTheApplicationNow(); 
       } 
      }; 

      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
      fragmentTransaction.add(0, request); 
      fragmentTransaction.commit(); 
     } 
     catch(Exception error) 
     { 
      Log.w("[NoodlePermissionGranter]", String.format("Unable to request permission: %s", error.getMessage())); 
      UnityPlayer.UnitySendMessage(UNITY_CALLBACK_GAMEOBJECT_NAME, UNITY_CALLBACK_METHOD_NAME, PERMISSION_DENIED); 
     } 
    } 

} 

NoodlePermissionGranter.sh

export JAVA_HOME=/Library/Java/JavaVirtualMachines/1.6.0.jdk/Contents/Home 
ClASSPATH="classes.jar" 

javac NoodlePermissionGranter.java -bootclasspath android.jar -classpath $ClASSPATH -d . 
javap -s com.noodlecake.unityplugins.NoodlePermissionGranter 
jar cvfM NoodlePermissionGranter.jar com/ 
rm -rf com 
read -p "Press any key to continue... " -n1 -s 

EDIT

logcat的

04-01 16:13:44.656 5504 5518 I NoodlePermissionGranter: grantPermission 1 
04-01 16:13:44.657 5504 5504 I NoodlePermissionGranter: fragment start 
04-01 16:13:44.657 5504 5504 I NoodlePermissionGranter: fragment start android.permission.READ_PHONE_STATE 
04-01 16:13:44.658 5504 5518 I NoodlePermissionGranter: grantPermission 0 
04-01 16:13:44.659 5504 5518 I NoodlePermissionGranter: grantPermission 2 
04-01 16:13:44.667 5504 5504 I NoodlePermissionGranter: fragment start 
04-01 16:13:44.667 5504 5504 I NoodlePermissionGranter: fragment start android.permission.WRITE_EXTERNAL_STORAGE 
04-01 16:13:44.675 5504 5504 I NoodlePermissionGranter: fragment start 
04-01 16:13:44.675 5504 5504 I NoodlePermissionGranter: fragment start android.permission.READ_EXTERNAL_STORAGE 
04-01 16:13:44.906 5504 5504 I NoodlePermissionGranter: onRequestPermissionsResult 
04-01 16:13:44.907 5504 5504 I NoodlePermissionGranter: PERMISSION_DENIED 
04-01 16:13:44.907 5504 5504 I NoodlePermissionGranter: onRequestPermissionsResult 
04-01 16:13:44.907 5504 5504 I NoodlePermissionGranter: PERMISSION_DENIED 
04-01 16:13:44.907 5504 5504 I NoodlePermissionGranter: onRequestPermissionsResult 
04-01 16:13:44.907 5504 5504 I NoodlePermissionGranter: PERMISSION_DENIED 

回答

0

請務必檢查您的Android清單有危險的權限!

這是我的問題。