0

What's the difference between creating the object using AndroidJavaObject and AndroidJNI.NewObject?

new AndroidJavaObject("android.content.Intent", ACTION_SEND);

vs

IntPtr intentClass = AndroidJNI.FindClass("android.content.Intent");
IntPtr intentConstructor = AndroidJNI.GetMethodID(IntentClass,"<init>","(Ljava/lang/String;)V");


jvalue[] intentParameters = new jvalue[1];
intentParameters[0] = ACTION_SEND;
IntPtr intentObject = AndroidJNI.NewObject(intentClass, intentConstructor, intentParameters);

Is AndroidJavaObject just a shorter version or there are any downsides?

2
  • Perhaps the AndroidJavaObject constructor always calls FindClass internally without any kind of caching. There are times where you might prefer just calling FindClass once and saving the result as a global reference for repeated use (e.g. trying to use FindClass for non-system classes on a purely native thread can be problematic). Commented Jul 20, 2018 at 9:14
  • @Michael yeh, inside it does the same thing. Commented Jul 20, 2018 at 9:21

1 Answer 1

2

If you decompile this class, you will see that it does the same thing:

private void _AndroidJavaObject(string className, params object[] args)
{
  this.DebugPrint("Creating AndroidJavaObject from " + className);
  if (args == null)
    args = new object[1];
  using (AndroidJavaObject @class = AndroidJavaObject.FindClass(className))
  {
    this.m_jclass = new GlobalJavaObjectRef(@class.GetRawObject());
    jvalue[] jniArgArray = AndroidJNIHelper.CreateJNIArgArray(args);
    try
    {
      IntPtr num = AndroidJNISafe.NewObject((IntPtr) this.m_jclass, AndroidJNIHelper.GetConstructorID((IntPtr) this.m_jclass, args), jniArgArray);
      this.m_jobject = new GlobalJavaObjectRef(num);
      AndroidJNISafe.DeleteLocalRef(num);
    }
    finally
    {
      AndroidJNIHelper.DeleteJNIArgArray(args, jniArgArray);
    }
  }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.