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:
Function callback () luajava. loadlib ("CN. HPC. common. lua. loadlibexample "," open ") -- call CN. HPC. common. the open method of the loadlibexample class in the Lua package. Javaobjcet. methodopen ("1", 4) -- returns two javaobjcet parameters. methodopen ("2", "blog", "http://blog.csdn.net/hpccn") -- returns three 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
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:
11-19 11:12:32. 783: I/system. out (4137): Userdata11-19 11:12:32. 783: I/system. out (4137): CN. HPC. common. lua. loadlibexample $1 @ 2bc4889011-19 11:12:32. 793: I/system. out (4137): 1.011-19 11:12:32. 793: I/system. out (4137):-4.011-19 11:12:32. 793: I/system. out (4137): Userdata11-19 11:12:32. 793: I/system. out (4137): CN. HPC. common. lua. loadlibexample $1 @ 2bc4889011-19 11:12:32. 793: I/system. out (4137): 2.011-19 11:12:32. 793: I/system. out (4137): blog11-19 11:12:32. 793: I/system. out (4137): http://blog.csdn.net/hpccn11-19 11:12:32. 793: I/system. out (4137): Userdata11-19 11:12:32. 793: I/system. out (4137): CN. HPC. common. lua. loadlibexample $1 @ 2bc4889011-19 11:12:32. 803: I/system. out (4137): 3.011-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