Java code calling using lua on Android platform

Source: Internet
Author: User

Java code calling using lua on Android platform

Dynamic languages are flexible, configurable, and convenient for debugging, which can greatly facilitate development. If used, the development efficiency can be greatly improved.


No wonder there is no script language integrated in complex software development such as game development.


Among them, lua is small, flexible, easy to expand, and can be easily embedded in most game development.


For me, it is very important for a person who fully understands the power of dynamic to integrate a scripting language in software development.


However, on the Android platform, after trying Python, I found that although integration is not difficult, it is still a bit large, and there are more than 3 m dynamic libraries, you have to crop the python library by yourself,


Compilation is not very convenient. So recently, I have been focusing on lua. Compared with lua, although lua is not as pure as Python, it seems that this disadvantage has become its advantage.


I have to say that on the Android platform, lua is more practical than python. The so-called, there is no best, only the most suitable. While pursuing purity, Python also hinders its development.


For every developer who regards efficiency as life, convenience and practicality are the king.


This means that you can complete software development as soon as possible, reduce the risk of software development, reduce the development pressure, and spend less time with your girlfriend (HA, programmers have girlfriends ).


Go back to the title to see how to integrate lua and java on the Android platform.


Luajava has been transplanted on the android platform. The code can be downloaded from https://github.com/mkottman/androlua.


As for compiling and adding luajava to your project, you can refer to the AndroLua code. The following describes how to use lua to interact with android:



The simplest example is:


function launchSetting(context)    intent = luajava.newInstance("android.content.Intent")    c = luajava.newInstance("android.content.ComponentName","com.android.settings", "com.android.settings.Settings")    intent:setFlags(intent.FLAG_ACTIVITY_NEW_TASK);    intent:setComponent(c)    context:startActivity(intent)end






This example can be used to start "Settings"


One line is explained as follows:

    intent = luajava.newInstance("android.content.Intent")


Create an Intent instance: equivalent to the Intent intent = new Intent () in Java ()


    c = luajava.newInstance("android.content.ComponentName","com.android.settings", "com.android.settings.Settings")


Create a ComponentName instance, which is equivalent to the Java code: ComponentName c = new ComponentName ("com. android. settings", "com. android. settings. Settings ")


Intent: setFlags (intent. FLAG_ACTIVITY_NEW_TASK );
Start a new Activity instance


Intent: setComponent (c)
Context: startActivity (intent)
Start Activity




At the beginning, lua students may be unfamiliar with "." and. "." Is a method or variable used to call a class (static method), and ":" is a method used to call an object.
See the following line of code:
Intent: setFlags (intent. FLAG_ACTIVITY_NEW_TASK );
Because FLAG_ACTIVITY_NEW_TASK is a static variable of Intent, it must be called as follows:


Intent. FLAG_ACTIVITY_NEW_TASK


If it is written:


Intent: FLAG_ACTIVITY_NEW_TASK


It is wrong.


Also, the above Code is equivalent to the following code:
Intent. setFlags (intent, intent. FLAG_ACTIVITY_NEW_TASK );






Let's look at an example:


require 'import'button_cb = {}function button_cb.onClick(ev)    print('hello,world')    launchSetting(activity)endlocal id = luajava.bindClass("sk.kottman.androlua.R$id")local launch = activity:findViewById(id.launchButton)buttonProxy = luajava.createProxy("android.view.View$OnClickListener", button_cb)launch:setOnClickListener(buttonProxy)




This example shows how to use luajava. createProxy. This example registers a Listener for a Click.


Note the following code:
Local id = luajava. bindClass ("sk. kottman. androlua. R$ id ")
Local launch = activity: findViewById (id. launchButton)


Because id is a static class in R. java, the following code is used to reference the resource file of the Button:


Id. launchButton


Instead of id: launchButton


In addition, pay attention to the class reference methods in the class, such:


Android. view. View $ OnClickListener











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.