【发布时间】:2015-11-02 17:22:54
【问题描述】:
我找不到如何在 C# 中从 AndroidJavaObject 获取 ListArray 变量。
我正在尝试使用Count 为ListArray 中存储为AndroidJavaObject 的项目数创建for 函数。但我需要知道如何从AndroidJavaObject 获取Count 以及如何正确使用它。
这就是我用来获取变量的方法,还要注意“包”不是AndroidJavaObject[]。
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
int flag = new AndroidJavaClass("android.content.pm.PackageManager").GetStatic<int>("GET_META_DATA");
AndroidJavaObject pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject packages = pm.Call<AndroidJavaObject>("getInstalledApplications", flag);
目前还很初级,但感谢How to get installed apps list with Unity3D? 的一些帮助,它可以工作
到目前为止的进展在获取图标时停滞不前,其他一切都很完美,我需要一种方法来获取以某种方式链接到图标的 string,或者获取链接的 Texture2D图标。
另一种选择是使用包含drawable 的AndroidJavaObject,就好像它是一个Texture2D。我不知道如何完成这些。
我的另一个想法是将它转换为另一个变量,例如 byte[] 可以转移和转换回来,但我还没有找到一种方法在这种情况下工作。
int count = packages.Call<int>("size");
AndroidJavaObject[] links = new AndroidJavaObject[count];
string[] names = new string[count];
Texture2D[] icons = new Texture2D[count];
int ii =0;
for(int i=0; ii<count;){
//get the object
AndroidJavaObject currentObject = packages.Call<AndroidJavaObject>("get", ii );
try{
//try to add the variables to the next entry
links[i] = pm.Call<AndroidJavaObject>("getLaunchIntentForPackage", currentObject.Get<AndroidJavaObject>("processName"));
names[i] = pm.Call<string>("getApplicationLabel", currentObject);
icons[i] = pm.Call<Texture2D>("getApplicationIcon", currentObject);
Debug.Log ("(" + ii + ") " + i +" "+ names[i]);
//go to the next app and entry
i++;
ii++;
}
catch
{
//if it fails, just go to the next app and try to add to that same entry.
Debug.Log("skipped "+ ii);
ii++;
}
}
我真的很讨厌在这里连续问两个问题,我很抱歉不得不这样做,但这是一个困难且看似尴尬的情况,几乎没有文档(我可以找到)。
【问题讨论】:
标签: java c# android arraylist unity3d