Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get variables from AndroidJavaObject into a C# class using Unity3D

I can't find how to get ListArray variables from an AndroidJavaObject in C#.
I'm trying to make a for function using a Count for the number of items in the ListArray that is stored as an AndroidJavaObject. But I need to know how to get the Count from the AndroidJavaObject and how to use it properly.
This is what I'm using to get the variables, also notice that "packages" is not an 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);

it's very rudimentary at this point, but it works thanks to some help from this How to get installed apps list with Unity3D?

The progress thus far stalls at getting the icon, everything else works perfect, I need a way to get either a string that links to the icon somehow, or the Texture2D of the icon.
Another alternative would be a to use a AndroidJavaObject that contains a drawable as if it's a Texture2D. I have no idea how to accomplish any of this though.
Another Idea I had was to convert it to another variable, like byte[] that can be transferred and converted back, but I have yet to find a method of that which works under the circumstance.

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++;
    }

}

I really hate to ask two questions in a row here, and I apologize for having to do so, but this is a difficult and seemingly awkward circumstance, that has little to no documentation (that I can find).

like image 576
Jonathan C Avatar asked Nov 26 '25 20:11

Jonathan C


1 Answers

First of all the docs on the interop between Unity3D and Android are scarce at best.

The most important thing is that the only interface Unity exposes to work with java object is AndroidJavaObject and that interface has only a couple of methods defined. You can only use those ones and only those.

This means that you don't get a Count object when working with an java array and you'll still be working with AndroidJavaObject.

int count = packages.Call<int>("size"); //getting the size of the array

for( int i = 0; i < count; i++ )
{
 //getting the object at location i
 AndroidJavaObject currentObject = packages.Call("get", i );

 //use currentObject using the same methods as before: Call, CallStatic, Get, GetStatic
}

I know this is verbose, and writing code like this is hard to test, you need to make and apk and deploy it to a device to check that it runs.

Probably a faster way of doing this is to make your own java class that does all this on the java side and expose one method that gets called from the Unity side. This way at least you get the benefit of static typing when making a jar that you'll add in the Plugins/Android folder.

like image 94
Radu Diță Avatar answered Nov 29 '25 09:11

Radu Diță



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!