Send an activity from C# to Java

Hi Everyone!

I have a problem sending a variable of type Activity from C# to Java.

The following source code is what I did in C#:

public static string TestJava()
	{
		string somestring = "";
		
		playerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
		activity = playerClass.GetStatic<AndroidJavaObject>("currentActivity");
		pluginClass = new AndroidJavaClass("com.Vector3GameStudio.GoogleMobileAds.GoogleMobileAdsActivity");
		
		try
		{
			pluginClass.CallStatic("Test", activity );
		}
		catch
		{
			somestring += "ERROR invoking Test";
		}
		
        return somestring;
	}

And the following source code is the Java method who it will recieve the Activity.

public static void Test(Activity currentActivity)
{
        // Doesn't get here at all
}

If I change the parameter Activity in the Java method by String, int and so on, the code works well but if I change it by an Activity, the java invocation doesn’t work.

Could someone help me with that?

Thank you in advance!

Your code looks OK, but you didn’t post all of it, so hard to say. I suggest turning on JNI debugging. Add this line at the startup of your main scene (before any other JNI calls are made):

AndroidJNIHelper.debug = true;

This should output very detailed logs of exactly what’s happening behind the scenes of the JNI calls.

Hi vladimirg,

This is the message threw in the exception:

Tetsjava.lang.NSuchMethodError: no static method match with name=‘Test’ signature=‘(Loomunity3d.player.UnityPlayerActivity;)V’ in class

I don’t know what happened with my code, but I think that it’s something with casting or parsing a variable of type Activity to UnityPlayerActivity, but I’m not sure about it :frowning:

If you change your Java method signature to this:

public static void Test(int i)

And change the C# code to this:

pluginClass.CallStatic("Test", 1);

Does it work?

Yes, if I change my method signature as in your example It works perfectly, but If I try to send an Activity throws the exception that I referred to.

This means that the JNI method lookup is invariant. Which seems to me extremely naive, but what do I know. Let’s try getting the method ID and calling that method directly. In your C# code do this (code not tested, slight modifications may be required):

IntPtr pluginClassPtr = pluginClass.GetRawClass(); // May need change GetRawClass to GetRawObject, but try it like this first
IntPtr methodPtr = AndroidJNIHelper.GetMethodID(pluginClassPtr, "Test", "(Landroid.app.Activity;)V", true );
AndroidJNI.CallStaticVoidMethod(pluginClassPtr, methodPtr, AndroidJNIHelper.CreateJNIArgArray(new object[] {activity}));

This code tries to use more basic JNI calls to force the method call to happen, even though the types don’t match exactly. May need to be tweaked a bit to actually work, since I never needed to drop down to that lower-level. But it conceptually should work.

Thank you vladimirg! I’m going to change my code with your advices but, Why does it not work my code well? I’ve seen some people doing exactly the same than me, and theirs codes work well for them :frowning: :frowning: :frowning: :frowning: :frowning: .

I don’t know if it’s something with the manifiest, the ADT (or Eclipse) configuration or something else.

When I finished to do your recommendations I’ll come back to post if It works or not my code.

Hi,

I changed my source code with yours, and now It doesn’t appear the exception and neither does the logic of the test method.

So, in my C# code I wrote the following:

public static string TestJava()
	{
		string somestring = "";
		
		AndroidJNIHelper.debug = true; 
		
		activity	= playerClass.GetStatic<AndroidJavaObject>("currentActivity");
		
		// May need change GetRawClass to GetRawObject, but try it like this first
		IntPtr pluginClassPtr = pluginClass.GetRawObject();
		IntPtr methodPtr = AndroidJNIHelper.GetMethodID(pluginClassPtr, "Test", "(Landroid.app.Activity;)V", true );
		
		try
		{
			AndroidJNI.CallStaticVoidMethod(pluginClassPtr, methodPtr, AndroidJNIHelper.CreateJNIArgArray(new object[] {activity}));
		}
		catch (Exception e)
		{
			somestring += "ERROR invoking Test" + e.Message;
		}
		
        return somestring;
	}

And in this is my Java Code:

public static void Test(Activity a)
	{
		Toast.makeText(a.getApplicationContext(), "Hello", Toast.LENGTH_SHORT).show();
	}

That’s all, I’m really frustrated right now :face_with_spiral_eyes:

The code you posted uses GetRawObject(), did you try GetRawClass() as well? Did you inspect the IntPtr objects to have normal-looking pointers (not nulls or whatever)?

Yes, I used both (GetRawObject and GetRawClass) and I checked the pointers too. I don’t know what’s going on, Have you ever done a Java plugin for Unity3D before? I need some advices with my code :S

Hi Everybody,

In the following link I’ve uploaded the ADT project and the Unity3D project http://vector3gamestudio.com/Downloads/Advertising%20Unity3D%20Android%20Plugin.zip

And the following snapshot is the exception thrown in the Android device.

If someone could help me, I would appreciate it so much.

Greetings!