Lua learning Note 1 Android calls Lua and starts intent in Lua script

Source: Internet
Author: User

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.

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.