Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to pass parameter to java method from c# in JNI

I am working in Unity3D and writing my script in C#. I want to call my java method from c# script which take a parameter of boolean type but i don't know how to pass parameter from C# using JNI. I am able to call methods which does not take any parameter. This is what i have tried for calling methods which does not take any parameter and it is working fine.

private int BtnMyAppWall;
// create a JavaClass object...
        IntPtr cls_JavaClass    = JNI.FindClass("com/example/unitybuttontry/MainActivity");
        int mid_JavaClass       = JNI.GetMethodID(cls_JavaClass, "<init>", "(Landroid/app/Activity;)V");
        IntPtr obj_JavaClass    = JNI.NewObject(cls_JavaClass, mid_JavaClass, obj_Activity);
        Debug.Log("JavaClass object = " + obj_JavaClass);

        // create a global reference to the JavaClass object and fetch method id(s)..
        JavaClass           = JNI.NewGlobalRef(obj_JavaClass);

BtnMyAppWall    = JNI.GetMethodID(cls_JavaClass, "myAppWall", "()Ljava/lang/String;");

// get the Java String object from the JavaClass object
        IntPtr str_cacheDir     = JNI.CallObjectMethod(JavaClass, BtnMyAppWall);

Now if i want to pass a parameter of boolean type then i need to change my last two statement something like this:

  Stmt 1:  BtnMyAppWall = JNI.GetMethodID(cls_JavaClass, "myAppWall", "(Z)Ljava/lang/String;");

  Stmt 2:  // get the Java String object from the JavaClass object
            IntPtr str_cacheDir     = JNI.CallObjectMethod(JavaClass, BtnMyAppWall,true);

You can see in Stmt 1, i have mentioned 'Z' which means my method will take boolean parameter but in Stmt2 when i pass my value then it gives error:

Error: cannot convert object' expression to typeSystem.IntPtr'

Please help me out.

like image 758
Kapil Avatar asked Dec 26 '13 18:12

Kapil


1 Answers

You need to use Android.Runtime.JValue:

// get the Java String object from the JavaClass object
IntPtr str_cacheDir = JNI.CallObjectMethod(obj_JavaClass, BtnMyAppWall, new JValue(true));
like image 82
jonp Avatar answered Oct 09 '22 13:10

jonp