using System.Collections.Generic; using UnityEngine; namespace Unity.XR.PICO.TOBSupport { public class Intent { private string Componentpkg = ""; private string Componentcls = ""; private string Action = ""; private string type = ""; private string url = ""; List categoryList = new List(); Dictionary stringPairs = new Dictionary(); Dictionary boolPairs = new Dictionary(); Dictionary intPairs = new Dictionary(); Dictionary floatPairs = new Dictionary(); Dictionary doublePairs = new Dictionary(); public void setComponent(string pkg, string cls) { Componentpkg = pkg; Componentcls = cls; } public void setAction(string _Action) { Action = _Action; } public void setType(string _type) { type = _type; } public void setData(string _url) { url = _url; } public void addCategory(string _category) { categoryList.Add(_category); } public void putExtra(string name, string value) { stringPairs.Add(name, value); } public void putExtra(string name, int value) { intPairs.Add(name, value); } public void putExtra(string name, float value) { floatPairs.Add(name, value); } public void putExtra(string name, double value) { doublePairs.Add(name, value); } public void putExtra(string name, bool value) { boolPairs.Add(name, value); } public AndroidJavaObject getIntent() { AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent"); if (!string.IsNullOrEmpty(Componentpkg) && !string.IsNullOrEmpty(Componentcls)) { AndroidJavaObject componentName = new AndroidJavaObject("android.content.ComponentName", Componentpkg, Componentcls); intent.Call("setComponent", componentName); } if (!string.IsNullOrEmpty(Action)) { intent.Call("setAction", Action); } if (!string.IsNullOrEmpty(type)) { intent.Call("setType", type); } // mIntent.setData(Uri.parse("")); if (!string.IsNullOrEmpty(url)) { AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri"); // 对应的安卓调用函数是Uri.parse() AndroidJavaObject uriObject = uriClass.CallStatic("parse", url); intent.Call("setData", uriObject); } if (categoryList.Count != 0) { for (int i = 0; i < categoryList.Count; i++) { intent.Call("addCategory", categoryList[i]); } } foreach (KeyValuePair kvp in stringPairs) { intent.Call("putExtra", kvp.Key, kvp.Value); } foreach (KeyValuePair kvp in intPairs) { intent.Call("putExtra", kvp.Key, kvp.Value); } foreach (KeyValuePair kvp in boolPairs) { intent.Call("putExtra", kvp.Key, kvp.Value); } foreach (KeyValuePair kvp in floatPairs) { intent.Call("putExtra", kvp.Key, kvp.Value); } foreach (KeyValuePair kvp in doublePairs) { intent.Call("putExtra", kvp.Key, kvp.Value); } return intent; } } }