- Home /
How to use AndroidJavaClass.Call() to pass a ArrayList or Array
Hi all,
I've not had much experience to play around with the JNI bridge.
I previously had a problem that not being able to pass a object to a Java method and @liortal help me answer the question by adding another constructor in Java Class . http://answers.unity3d.com/questions/1237659/how-to-use-androidjavaclasscall-to-pass-a-data-obj.html#answer-form
What I need to achieve is basically pass the arraylist or array from C# to the method in the Android Java Class. Thank you.
private AndroidJavaClass ajc;
private AndroidJavaObject ajo;
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaClass videoClass = new AndroidJavaClass("com.example.sdk.VidoSchema);
ajo = jc.GetStatic<AndroidJavaObject>("currentActivity");
ajc = new AndroidJavaClass("com.example.sdk.ExampleSDK");
Now I can pass the object to Java class by this:
private AndroidJavaObject obj = new AndroidJavaObject ("com.example.sdk.VidoSchema", "1", "2","3","4","5");
ajc.CallStatic ("lanuchSDKMethod", ajo, SERIAL, obj);
Above code will call this method in java:
public static void lanuchSDKMethod(Activity activity, String serialKey, VidoSchema obj) {
...
...
}
My question is: How to pass the arraylist or array of this VidoSchema object?
AndroidJavaClass VidoSchema = new AndroidJavaClass ("com.example.sdk.VidoSchema");
ArrayList <VidoSchema> arraylist = new ArrayList ();
arraylist.add(obj1);
arraylist.add(obj2);
ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,arraylist);
In java ExampleSDK file, I have methods:
public static void lanuchSDKMethod(Activity activity, String serialKey, ArrayList<VidoSchema> cArrayList) {
...
...
}
This is the data structure class:
public class VidoSchema implements Parcelable {
public boolean isEntryVideo;
public String branchId;
public String video_location;
public String xml_location;
public String subtitle_location;
public String assets_folder;
public VidoSchema(Parcel in) {
isEntryVideo = in.readByte() != 0x00;
branchId = in.readString();
xml_location = in.readString();
subtitle_location = in.readString();
assets_folder = in.readString();
}
public VidoSchema() {
}
public VidoSchema(boolean isEntryVideo, String branchId,
String video_location,
String xml_location, String subtitle_location,
String assets_folder) {
this.isEntryVideo = isEntryVideo;
this.branchId = branchId;
this.video_location = video_location;
this.xml_location = xml_location;
this.subtitle_location = subtitle_location;
this.assets_folder = assets_folder;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isEntryVideo ? 0x01 : 0x00));
dest.writeString(branchId);
dest.writeString(xml_location);
dest.writeString(subtitle_location);
dest.writeString(assets_folder);
}
@SuppressWarnings("unused")
public static final Creator<VidoSchema> CREATOR = new Creator<VidoSchema>() {
@Override
public VidoSchema createFromParcel(Parcel in) {
return new VidoSchema(in);
}
@Override
public VidoSchema[] newArray(int size) {
return new VidoSchema[size];
}
};
}
Answer by liortal · Sep 01, 2016 at 08:20 PM
It is possible to do these things through C#, but it can get VERY cumbersome to initialize all these Java types from C# code.
The better way would be to redesign your API such that your C# code calls into a "bridge" layer in Java - one that has a very simple API (e.g: no parameters or only simple types). From that bridge layer, you will call into whatever Java methods you need, passing in any type that is required.
If you still need to pass in types such as ArrayList from C# --> Java, you should follow the pattern that i described in the other answer i gave:
Figure out the name of the type you want to pass (package name + class name)
Figure out the constructor you want to invoke in order to create the type (what parameters it accepts)
Constructor the object instance by creating a new AndroidJavaObject("fullname", params here);
Pass this AndroidJavaObject into methods that accept that type in Java.
Thank you liortal. You are right. Create the Java methods is a better solution which make life easier.
please mark this as the answer if you think this answer can help others.
Your answer
Follow this Question
Related Questions
How to use AndroidJavaClass.Call() to pass a data object 1 Answer
How to add/edit onActivityResult for the activity created by currentActivity 1 Answer
How to call an android notification plugin if it's not the main activity? 1 Answer
CreateJavaRuntime problem 0 Answers
JNI Exception using Android Plugin 1 Answer