- Home /
How to use AndroidJavaClass.Call() to pass a data object
Hi, All,
I've not had much experience to play around with the JNI bridge. Now I have a problem that not being able to pass a customized Class to a Java method. What I need to achieve is basically pass the info 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 videoObj = new AndroidJavaClass("com.example.sdk.VidoSchema);
ajo = jc.GetStatic<AndroidJavaObject>("currentActivity");
ajc = new AndroidJavaClass("com.example.sdk.ExampleSDK");
How to pass the VidoSchema object to the method in java file?
ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObj);
Also, how to pass the arraylist of this VidoSchema class?
ArrayList<VidoSchema> videoObjArrayList;
ajc.CallStatic("lanuchSDKMethod", ajo, SERIAL,videoObjArrayList);
In java ExampleSDK file, I have methods:
public static void lanuchSDKMethod(Activity activity, String serialKey, VidoSchema cArrayList) {
...
...
}
public static void lanuchSDKMethod(Activity activity, String serialKey, ArrayList<VidoSchema> cArrayList) {
...
...
}
**This is the data structure file:**
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() {
}
@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 05:57 AM
First, you should understand the difference between AndoidJavaObject and AndroidJavaClass:
An AndroidJavaObject is a representation of an object instance in the JVM. When you create an AndroidJavaObject, you are essentially calling the Java constructor of that type (that matches the signature according to the parameters you pass in), then you can call methods and access fields of that object instance.
An AndroidJavaClass is the "type" representation of a JVM class. This is similar to the Type class in C#. Using this, you can query the class's members (static members) or access "reflection-like" information about the type (not a specific instance of that type!).
With this knowledge, let's see how to invoke your Java method:
private AndroidJavaClass ajc;
private AndroidJavaObject ajo;
// For static members you can use the AndroidJavaClass
// (you are not accessing any instance of the class)
AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
ajo = jc.GetStatic<AndroidJavaObject>("currentActivity");
// Again, this is OK in case the methods you are trying to call are static
var exampleSDK = new AndroidJavaClass("com.example.sdk.ExampleSDK");
// Construct a new VideoSchema object. This is equivalent to calling the empty ctor
// If you need to use another ctor, add the parameters after the type name:
// var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema", "1", "2");
var videoObj = new AndroidJavaObject("com.example.sdk.VidoSchema");
exampleSDK.CallStatic("lanuchSDKMethod", ajo, SERIAL, videoObj);
The difference doesn't makes sense to me either. $$anonymous$$ore specifically, I see no use for AndroidJavaClass.
The signature of the classes are:
public class AndroidJavaObject : IDisposable{...}
public class AndroidJavaClass : AndroidJavaObject
{
public AndroidJavaClass(string className);
}
So, one can see that AndroidJavaClass inherits from AndroidJavaObject, and have no overrides. Besides, one can call static methods also from AndroidJavaObject.
I agree that it's actually a bit confusing, not all classes or APIs are the best example of software engineering principles.
AndroidJavaClass and AndroidJavaObject are just wrapper classes. The important difference is in the way it's initialized. The full class looks like this:
public class AndroidJavaClass : AndroidJavaObject
{
public AndroidJavaClass(string className)
{
this._AndroidJavaClass(className);
}
internal AndroidJavaClass(IntPtr jclass)
{
if (jclass == IntPtr.Zero)
{
throw new Exception("JNI: Init'd AndroidJavaClass with null ptr!");
}
this.m_jclass = AndroidJNI.NewGlobalRef(jclass);
this.m_jobject = IntPtr.Zero;
}
private void _AndroidJavaClass(string className)
{
base.DebugPrint("Creating AndroidJavaClass from " + className);
using (AndroidJavaObject androidJavaObject = AndroidJavaObject.FindClass(className))
{
this.m_jclass = AndroidJNI.NewGlobalRef(androidJavaObject.GetRawObject());
this.m_jobject = IntPtr.Zero;
}
}
}
The AndroidJavaClass has a different constructor and actually uses the JNI interface to actually get the class reference. Also as you can see the "m_jobject" pointer is not set as the instance only references the class and not an instance.
A class and an instance of a class are two different concepts and those two classes are simply wrapper for those two concepts.
So when you create an "AndroidJavaClass" instance you basically just creating a wrapper object which represents a certain Java class. When you create an AndroidJavaObject you also create a wrapper in C# but also an instance of the Java class inside the JV$$anonymous$$.
$$anonymous$$eep in $$anonymous$$d that both classes have different constructors!! AndroidJavaClass implements a new constructor that overrides the internal parameterless constructor of AndroidJavaObject. So both classes server different purposes but share some common fields / methods. It would have been a bit cleaner if they had implemented an a abstract base class for both, but since only the constructors are different this inheritance works just fine.
I$$anonymous$$O, the inheritance doesn't really work fine. For example, you can write something like this, which is pretty much meaningless:
new AndroidJavaClass("aa.bb.cc").Call("some$$anonymous$$ethod");
The JavaClass cannot access any instance members (fields, methods). It would've been cleaner to model these classes differently to reflect this concept.
Your answer
Follow this Question
Related Questions
How to add/edit onActivityResult for the activity created by currentActivity 1 Answer
How to use AndroidJavaClass.Call() to pass a ArrayList or Array 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