Lua learning Note 3 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:

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

Related Article

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.