Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by kevin321an · Sep 01, 2016 at 05:13 PM · androidjnibridge

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:

  1. Figure out the name of the type you want to pass (package name + class name)

  2. Figure out the constructor you want to invoke in order to create the type (what parameters it accepts)

  3. Constructor the object instance by creating a new AndroidJavaObject("fullname", params here);

  4. Pass this AndroidJavaObject into methods that accept that type in Java.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image kevin321an · Sep 02, 2016 at 02:44 PM 0
Share

Thank you liortal. You are right. Create the Java methods is a better solution which make life easier.

avatar image liortal kevin321an · Sep 10, 2016 at 07:30 PM 1
Share

please mark this as the answer if you think this answer can help others.

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges