如何让安装的应用程序列表与Unity3D? [英] How to get installed apps list with Unity3D?
问题描述
我试图在C#中使用Unity3D的机器人发射,一切都在地方,除了检测安装的应用程序。
I'm trying to make an android launcher with Unity3D in C#, Everything is in place except detection of installed apps.
我已经尝试了很多不同的东西,但他们都陷在同一个地方,访问 getInstalledApplications
在软件包管理系统
I have tried a lot of different things, but they all get stuck at the same place, accessing getInstalledApplications
in the PackageManager.
我觉得我已经成功地复制在C#中 ApplicationInfo
类,至少变量存储明智的,通过研究Android源,所以我不认为这是一个问题,至少目前还没有...
I think I have managed to replicate the ApplicationInfo
class in C#, at least variable storage wise, by studying the android source, so I don't think that's a problem, at least not yet...
简单地说,我需要的URI(或东西我可以用它来打开应用程序),应用程序的名称,该应用程序的图标(或者一个字符串,它的位置,或的Texture2D
本身),我尝试 ApplicationInfo
,因为它拥有一切,然后一些,但如果我必须让他们单独或通过另一种方法,这是完全罚款。
Simply put, I need the URI (or something I can use to open the app), the name of the app, and the icon of the app (either a string to its location, or the Texture2D
itself), I try for ApplicationInfo
because it has all that and then some, but if I have to get them individually or through another method, that's totally fine.
下面是什么,我现在所做的例子。
这种情况无论我是否使用类或对象。
Here is an example of what I am doing now. This happens regardless of whether I use a Class or an Object.
void Start () {
AndroidJavaClass pluginClass = new AndroidJavaClass("android.content.pm.PackageManager");
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
Debug.Log(currentActivity.Call<string>("getPackageName"));
int flag = pluginClass.GetStatic<int>("GET_META_DATA");
AndroidJavaClass syspackage = currentActivity.Call<AndroidJavaClass>("getPackageManager");
和这里的错误,我得到
AndroidJavaException: Java.lang.NoSuchMethodError: no method with name = 'getInstalledApplications' signature=Ljava/lang/Class;' in class Lcom.unity3d/player/UnityPlayerActivity;
我如何使这项工作?或者你可以告诉/告诉我,将实现这一目标的另一种方法?
How do I make this work? Or can you tell/show me another method that will accomplish the goal?
//修改
好吧,我这个搞砸了几个小时尝试所有可能的组合,并且我知道了。
//EDIT Okay I messed with this for HOURS trying every possible combination, and I got it.
AndroidJavaClass pluginClass = new AndroidJavaClass("android.content.pm.PackageManager");
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
int flag = pluginClass.GetStatic<int>("GET_META_DATA");
AndroidJavaObject pm = currentActivity.Call<AndroidJavaObject>("getPackageManager");
AndroidJavaObject syspackages = pm.Call<AndroidJavaObject>("getInstalledApplications", flag);
现在我得弄清楚如何获得我需要走出Java对象和成C#字符串和整数比这一个完全不同的问题字符串和整数....
Now I just gotta figure out how to get the strings and ints i need out of the Java object and into C# strings and ints which is a totally different problem than this....
推荐答案
正在传递一个字符串作为参数的getInstalledApplications而不是整数。
You are passing a string as the param for getInstalledApplications instead of an integer.
您需要得到你的手的一个实例的软件包管理系统,要做到这一点,你需要得到一个的活动,幸运的是,团结可以给你。
You need to get your hands on an instance of PackageManager, to do this you'll need to get an Activity, fortunately, Unity can give you that.
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = jc.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject packageManager = currentActivity.Call<AndroidJavaObject>("getPackageManager");
int flag = pluginClass.GetStatic<int>("GET_META_DATA");
AndroidJavaObject[] arrayOfAppInfo = packageManager.Call<AndroidJavaObject[]>("getInstalledApplications", flag);
这篇关于如何让安装的应用程序列表与Unity3D?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!