如何使用 Unity3D 获取已安装的应用程序列表? [英] How to get installed apps list with Unity3D?

查看:243
本文介绍了如何使用 Unity3D 获取已安装的应用程序列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 C# 中的 Unity3D 制作一个 android 启动器,除了检测已安装的应用程序外,一切都已就绪.

I'm trying to make an android launcher with Unity3D in C#, Everything is in place except detection of installed apps.

我尝试了很多不同的方法,但它们都卡在同一个地方,访问 PackageManager 中的 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?

//编辑好吧,我把这个搞砸了 HOURS,尝试了每一种可能的组合,我明白了.

//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.

您需要获得 PackageManager 的实例,为此您需要获得一个 Activity,幸运的是,Unity 可以为您提供.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆