2014-07-09
One, the Android class calls Lua and callbacks
Android calls the LUA function and passes the class as a parameter, and then the function of the callback class in Lua
Calling Lua
Mluastate = Luastatefactory.newluastate (); Mluastate.openlibs (); Mluastate.ldostring (Kklua.loadassetsstring ( Mcontext, "Lua/swallow.lua"));//convert Lua to String Mluastate.getfield (Luastate.lua_globalsindex, "luaupdate");// Get Functionmluastate.pushjavaobject (this) in LUA;//pass parameter Mluastate.call (1, 0);//one parameter, 0 return value
Interface for callbacks
public void Luadraw (int imageId, int. x, int y, int scale) {//Note must also be public, LUA can call to System.out.println ("ImageId, X, Y, SCA Le: "+ imageId +", "+ x +", "+ y +", "+ scale";}
LUA Code
function Luaupdate (Luacanvas) luacanvas:luadraw (0, 1, 2, 1) end
Ii. Introduction of Luajava (included in the Luajava library)
There is no class in Lua, there is a Luajava in the Luajava library that can create classes in Java and invoke them. Here are a few ways
①newinstance (ClassName, ...)
Description: You can create a Java class based on the class name and return a LUA variable corresponding to the Java class. This makes it possible to invoke the methods in Java directly in Lua.
obj = luajava.newinstance ("Java.lang.Object")--obj is now a reference to the new object--created and any of its methods Can is accessed.--this creates a string tokenizer to the "A,b,c,d"--string using "," as the token SEPARATOR.STRTK = Luaj Ava.newinstance ("Java.util.StringTokenizer", "A,b,c,d", ",") while Strtk:hasmoretokens () do print (STRTK: NextToken ()) End
②bindclass (ClassName)
Description: You can have the variables in LUA correspond to a Java class (a class, not an instance) so that you can create an instance with this variable from LUA and call a static class
SYS = Luajava.bindclass ("Java.lang.System") print (Sys:currenttimemillis ())--This prints the time returned by the FUNCTI On.
③new (Javaclass)
Description: This is created on the basis of ②
str = Luajava.bindclass ("java.lang.String") strinstance = luajava.new (str)
④createproxy (Interfacenames, Luaobject)
⑤loadlib (ClassName, MethodName)
These two methods are not available at this time, will be used in the future to introduce
Above, thank you!