Android calls Lua and Lua scripts start intent
Calls in Android
Try {mluastate. ldostring (readstream (getresources (). openrawresource (R. raw. testlua); // find the functioninluafile function mluastate. getfield (luastate. lua_globalsindex, "launchintent"); mluastate. pushjavaobject (getapplicationcontext (); mluastate. call (1, 0);} catch (exception e) {e. printstacktrace ();}
Readstream implementation
Private string readstream (inputstream is) {try {bytearrayoutputstream BO = new bytearrayoutputstream (); int I = is. Read (); While (I! =-1) {Bo. write (I); I = is. read ();} return Bo. tostring ();} catch (ioexception e) {log. E ("readstream", "failed to read file stream"); Return "";}}
In the android project Res/raw/New Lua script testlua. Lua
-- Start intentfunction launchintent (context) -- New a Java instance local intent = luajava. newinstance ("android. content. intent ") intent: addflags (0x10000000) intent: setaction (" android. intent. action. view ") -- bind a Java instance and call the static method local uri = luajava. bindclass ("android.net. uri ") intent: setdata (URI: parse (" http://blog.csdn.net/hpccn ") Context: startactivity (intent); End
Lua calls methods of static and non-static classes in Java.
Static class usage
uri = luajava.bindClass("android.net.Uri")
Non-static class
intent = luajava.newInstance("android.content.Intent")
Start an intent in the Lua script and open a webpage, which is my blog.