Lua learning notes android calls Lua. The Lua script calls back Java using LoadLib and transmits multiple parameters.

Source: Internet
Author: User

Lua learning Note 3 android calls Lua. The Lua script calls back Java using LoadLib and transmits multiple parameters.

Multiple parameters can be received in Java and each parameter is displayed.


The method for loading the Lua script in android is not the focus of this article. You can refer to the notes in front of this blog.


Lua script:


[Plain]
Function callback ()
Luajava. loadLib ("cn. hpc. common. lua. LoadLibExample", "open") -- call the open method of the LoadLibExample class in the cn. hpc. common. lua package.
JavaObjcet. methodOpen ("1", 4) -- two parameters are returned.
JavaObjcet. methodOpen ("2", "blog", "http://blog.csdn.net/hpccn") -- returns 3 parameters
JavaObjcet. methodOpen ("3", "different parameters", "http://blog.csdn.net/hpccn ")
End

Callback Interface
In the test, only the parameters passed in from Lua are output.

Package name: cn. hpc. common. lua

Class Name: LoadLibExample


Method Name: open


[Java]
Package cn. hpc. common. lua;
 
 
Import org. keplerproject. luajava. JavaFunction;
Import org. keplerproject. luajava. LuaException;
Import org. keplerproject. luajava. LuaState;
 
Public class LoadLibExample {
Public static int open (LuaState L) throws LuaException {
L. newTable ();
L. pushValue (-1 );
L. setGlobal ("javaObjcet ");
 
L. pushString ("methodOpen ");
 
L. pushJavaFunction (new JavaFunction (L ){
 
Public int execute () throws LuaException {
 
For (int I = 0, count = L. getTop () + 1; I <count; ++ I ){
System. out. println (getParam (I ));
}
Return 0;
}
});
 
L. setTable (-3 );
 
Return 1;
}
}

Running result, Log output:

[Plain]
11-19 11:12:32. 783: I/System. out (4137): Userdata
11-19 11:12:32. 783: I/System. out (4137): cn. hpc. common. lua. LoadLibExample $1 @ 2bc48890
11-19 11:12:32. 793: I/System. out (4137): 1.0
11-19 11:12:32. 793: I/System. out (4137):-4.0
11-19 11:12:32. 793: I/System. out (4137): Userdata
11-19 11:12:32. 793: I/System. out (4137): cn. hpc. common. lua. LoadLibExample $1 @ 2bc48890
11-19 11:12:32. 793: I/System. out (4137): 2.0
11-19 11:12:32. 793: I/System. out (4137): blog
11-19 11:12:32. 793: I/System. out (4137): http://blog.csdn.net/hpccn
11-19 11:12:32. 793: I/System. out (4137): Userdata
11-19 11:12:32. 793: I/System. out (4137): cn. hpc. common. lua. LoadLibExample $1 @ 2bc48890
11-19 11:12:32. 803: I/System. out (4137): 3.0
11-19 11:12:32. 803: I/System. out (4137): Different Parameters
11-19 11:12:32. 803: I/System. out (4137): http://blog.csdn.net/hpccn

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.