LUA Learning 2)--android and Lua intermodulation

Source: Internet
Author: User

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!

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.