代码之家  ›  专栏  ›  技术社区  ›  Suvitruf - Andrei Apanasik

如何在UnyTy3D中创建Java对象(使用ANDROIDJavaObjor或ANDROIDJNI.NeWObjt)?

  •  0
  • Suvitruf - Andrei Apanasik  · 技术社区  · 6 年前

    创建对象与使用 AndroidJavaObject 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);
    

    AndroidJavaO对象 只是一个较短的版本,还是有什么缺点?

    1 回复  |  直到 6 年前
        1
  •  1
  •   Suvitruf - Andrei Apanasik    6 年前

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