public static Dictionary <string, string> GetArguments() { try { using (var unityPlayer = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer")) using (var currentActivity = unityPlayer.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity")) using (var intent = currentActivity.Call <UnityEngine.AndroidJavaObject>("getIntent")) { var hasExtra = intent.Call <bool>("hasExtra", "arguments"); if (hasExtra) { using (var extras = intent.Call <UnityEngine.AndroidJavaObject>("getExtras")) { var arguments = extras.Call <string>("getString", "arguments").Split(' '); return(CommandLineUtility.ParseCommandLineArgs(arguments)); } } } } catch (Exception e) { UnityEngine.Debug.LogException(e); } return(new Dictionary <string, string>()); }
// Gets the Unity player activity. // For iOS, this returns Zero. private static IntPtr GetPlayerActivity() { #if UNITY_ANDROID UnityEngine.AndroidJavaClass jc = new UnityEngine.AndroidJavaClass( "com.unity3d.player.UnityPlayer"); return(jc.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity") .GetRawObject()); #else return(IntPtr.Zero); #endif }
public static void StartNsd() { #if __UNITY_ANDROID__ UnityEngine.AndroidJavaClass c = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); var context = c.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity"); var arg = new UnityEngine.AndroidJavaObject("java.lang.String", "servicediscovery"); context.Call <UnityEngine.AndroidJavaObject>("getSystemService", arg); context.Dispose(); arg.Dispose(); c.Dispose(); #endif }
static RegisterService() { UnityEngine.AndroidJavaClass c = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); var context = c.GetStatic<UnityEngine.AndroidJavaObject>("currentActivity"); if (context == null) { c.Dispose(); throw new Exception("Failed to get context"); } var arg = new UnityEngine.AndroidJavaObject("java.lang.String", "servicediscovery"); context.Call<UnityEngine.AndroidJavaObject>("getSystemService", arg); context.Dispose(); arg.Dispose(); c.Dispose(); }
/// <summary> /// 连接华为推送并获取token,必须集成华为推送SDK /// </summary> public static void InitHuaweiPush() { using (var actClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer")) { UnityEngine.AndroidJavaObject curActivityContext = actClass.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity"); if (curActivityContext != null) { UnityEngine.AndroidJavaClass clsSystemUtil = new UnityEngine.AndroidJavaClass("com.netease.hwpushwrapper.HWPush"); if (clsSystemUtil != null) { clsSystemUtil.CallStatic("initHuaweiPush", curActivityContext); UnityEngine.Debug.Log("call java method initHuaweiPush"); } else { UnityEngine.Debug.Log("can't find class com.netease.hwpushwrapper.HWPush"); } } } }
public static string GetAndroidExternalFilesDir() { using (UnityEngine.AndroidJavaClass unityPlayer = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer")) { using (UnityEngine.AndroidJavaObject context = unityPlayer.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity")) { // Get all available external file directories (emulated and sdCards) UnityEngine.AndroidJavaObject[] externalFilesDirectories = context.Call <UnityEngine.AndroidJavaObject[]>("getExternalFilesDirs", (object)null); UnityEngine.AndroidJavaObject emulated = null; UnityEngine.AndroidJavaObject sdCard = null; for (int i = 0; i < externalFilesDirectories.Length; i++) { UnityEngine.AndroidJavaObject directory = externalFilesDirectories[i]; using (UnityEngine.AndroidJavaClass environment = new UnityEngine.AndroidJavaClass("android.os.Environment")) { // Check which one is the emulated and which the sdCard. bool isRemovable = environment.CallStatic <bool>("isExternalStorageRemovable", directory); bool isEmulated = environment.CallStatic <bool>("isExternalStorageEmulated", directory); if (isEmulated) { emulated = directory; } else if (isRemovable && isEmulated == false) { sdCard = directory; } } } // Return the sdCard if available if (sdCard != null) { return(sdCard.Call <string>("getAbsolutePath")); } else { return(emulated.Call <string>("getAbsolutePath")); } } } }
static RegisterService() { Couchbase.Lite.Unity.UnityMainThreadScheduler.TaskFactory.StartNew(() => { UnityEngine.AndroidJavaClass c = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); var context = c.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity"); if (context == null) { c.Dispose(); throw new Exception("Failed to get context"); } var arg = new UnityEngine.AndroidJavaObject("java.lang.String", "servicediscovery"); context.Call <UnityEngine.AndroidJavaObject>("getSystemService", arg); context.Dispose(); arg.Dispose(); c.Dispose(); _primedEvent.Set(); }); }
private static void InitSystemUtil() { #if UNITY_ANDROID try { /* The Mono garbage collector should release all created instances of AndroidJavaObject and AndroidJavaClass after use, * but it is advisable to keep them in a using(){} statement to ensure they are deleted as soon as possible. * Without this, you cannot be sure when they will be destroyed. */ using (var actClass = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer")) { UnityEngine.AndroidJavaObject curActivityContext = actClass.GetStatic <UnityEngine.AndroidJavaObject>("currentActivity"); if (curActivityContext != null) { UnityEngine.AndroidJavaClass clsSystemUtil = new UnityEngine.AndroidJavaClass("com.netease.nimlib.NIMSDK"); if (clsSystemUtil != null) { NimUtility.Log.Info("com.netease.nimlib.NIMSDK found"); Boolean init = clsSystemUtil.CallStatic <Boolean>("init", curActivityContext, "fjni_wrapper"); NimUtility.Log.Info("init:" + init); //string androidId = clsSystemUtil.CallStatic<String>("getAndroidId"); //NimUtility.Log.Info("androidId:" + androidId); } } } } catch (System.Exception e) { System.Console.Write("initAndroidExceptionHandler failed, an unexpected error: " + e.ToString()); } #endif #if UNITY_IPHONE || UNITY_IOS if (UnityEngine.Application.platform == UnityEngine.RuntimePlatform.IPhonePlayer) { //TODO: } #endif }
static ServiceBrowser() { Couchbase.Lite.Unity.UnityMainThreadScheduler.TaskFactory.StartNew(() => { UnityEngine.AndroidJavaClass c = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"); var context = c.GetStatic<UnityEngine.AndroidJavaObject>("currentActivity"); if (context == null) { c.Dispose(); throw new Exception("Failed to get context"); } var arg = new UnityEngine.AndroidJavaObject("java.lang.String", "servicediscovery"); context.Call<UnityEngine.AndroidJavaObject>("getSystemService", arg); context.Dispose(); arg.Dispose(); c.Dispose(); _primedEvent.Set(); }); }