haXe NME通过JNI调用Android

Linux大全评论718 views阅读模式

haXe中的JNI概念

首先澄清一个概念问题,NME中的JNI接口和java中的含义并不相同,java中的JNI是java调用本地C/C++代码的标准接口,而haXe中的JNI则正好相反,是用haXe在Android目标平台调用java代码。当然,意义上也说得通,因为从haXe和Android目标的关系来说,Android自带的java运行库反而是native的嘛,呵呵。

nme.JNI类的使用

haXe NME调用java方法是通过nme.JNI类实现的,JNI类中只有两个静态函数createStaticMethod和createMemberMethod。看看函数的说明:

 /**
  * Create bindings to an class instance method in Java
  * @param className  The name of the target class in Java
  * @param memberName  The name of the target method
  * @param signature  The JNI string-based type signature for the method
  * @param useArray  Whether the method should accept multiple parameters, or a single array with the parameters to pass to Java
  * @return  A method that calls Java. The first parameter is a handle for the Java object instance, the rest are passed into the method as arguments
  */
 public static function createMemberMethod(className:String, memberName:String, signature:String, useArray:Bool = false):Dynamic;
 
 /**
  * Create bindings to a static class method in Java
  * @param className  The name of the target class in Java
  * @param memberName  The name of the target method
  * @param signature  The JNI string-based type signature for the method
  * @param useArray  Whether the method should accept multiple parameters, or a single array with the parameters to pass to Java
  * @return  A method that calls Java. Each argument is passed into the Java method as arguments
  */
 public static function createStaticMethod(className:String, memberName:String, signature:String, useArray:Bool = false):Dynamic;

可以看到,参数列表完全一致,这两个函数的区别就在于,memberMethod在调用时,第一个参数必须是java对象的句柄(handler),它也就是java方法中的this引用。

具体的代码例子请看下面代码:

public static function getBuffer():Dynamic
{
    if (_getBuffer_func == null)
        _getBuffer_func = nme.JNI.createStaticMethod("com/company/product/HaxeStub", "getBuffer", "()[I", true);
    var a = new Array<Dynamic>();
    return _getBuffer_func(a);
}

上面的haXe函数将调用java类com.company.product.HaxeStub的静态方法"int[] getBuffer()"。

注意:

1. 类名中的'.'必须用'/'代替,NME自带的例程中这里写错了(那个例程已经很久没有更新了)。

2. 所谓signature就是java方法的签名,也就是方法原型的表述方式,这是Java虚拟机的标准。上面的方法签名说明getBuffer的原型是int[] getBuffer()。具体细节这里有篇不错的文章可以看看http://www.linuxidc.com/Linux/2013-01/77231.htm

3. createStaticMethod返回的Dynamic对象就可以当做一个普通的haXe函数来调用了,haXe会自动帮你对参数和返回值做java <-> haXe的转换。

企鹅博客
  • 本文由 发表于 2020年8月17日 03:25:57
  • 转载请务必保留本文链接:https://www.qieseo.com/177166.html

发表评论