Launching Android activities from Lua

All things Storyboard for the Android platform.

Moderator: jamiev

Launching Android activities from Lua

Postby mlove » Tue May 07, 2013 2:08 pm

We've received a few questions about launching Android activities using Lua so I thought I'd post an example. This is going to be a simple example for viewing a website, but the process would be similar for other activities. The example uses an Intent to launch the browser (here's the documentation on Intents http://developer.android.com/training/basics/intents/index.html). The basic premise is that we need to tell the Intent what action it performs, what data it needs to perform that action (in this case a URI representing a URL), and then tell the native activity to start the intent. Here's the example script:

function browse(mapargs)
if (luajava == nil) then
-- No luajava Lua object
return
end

if (luajava.bindClass == nil) then
-- No luajava.bindClass Lua object
return
end

--Create a new java String object
local urlString = luajava.newInstance("java.lang.String", "http://www.cranksoftware.com/")
if (urlString == nil) then
-- No java.lang.String object
return
end

local uriClass = luajava.bindClass("android.net.Uri")
if (uriClass == nil) then
-- No java.net.URI object
return
end

-- Parse the url String to get the appropriate URI
local urlURI = uriClass:parse(urlString)
if (urlURI == nil) then
-- No java.net.URI object
return
end

local intentClass = luajava.bindClass("android.content.Intent")
if (intentClass == nil) then
-- No intent class
return
end

--Create a java String representing a view action
local intentString = luajava.newInstance("java.lang.String", "android.intent.action.VIEW")
if (intentString == nil) then
-- No intent ACTION_VIEW string
return
end

--Create a new Intent object
local intent = luajava.newInstance("android.content.Intent")
if (intent == nil) then
-- No intent object
return
end

intent:setAction(intentString)
intent:setData(urlURI)
local na = luajava.nativeActivity()
if (na ~= nil) then
-- Browse
na:startActivity(intent)
else
-- No Native Activity
end
end

Hopefully this will help people get started with integrating other Android activities within their Storyboard apps.

- Matt L.
mlove
 
Posts: 22
Joined: Tue Apr 17, 2012 1:26 pm

Re: Launching Android activities from Lua

Postby mlove » Tue May 07, 2013 3:17 pm

I thought I would post an additional example of launching another android app using an Intent. This is less straight forward than launching an action as it requires some knowledge about the app you want to launch. Here's a good explanation of the process http://ashwinrayaprolu.wordpress.com/2011/03/17/launching-different-applications-from-android-activity-using-intent/. Since we're using LuaJava to launch the app our example will look a bit different. This example launches the Logitech Harmony Remote app but could be used for any app as long as we know the app's package name (in this case "com.logitech.harmoneylink"), and it's Activity (in this case the starting activity is "com.logitech.harmony.ui.SplashActivity"). I got both of these pieces of information by running adb logcat and launching the app.

function launch_harmony()
if (luajava == nil) then
return
end

if (luajava.bindClass == nil) then
return
end

local intentClass = luajava.bindClass("android.content.Intent")
if (intentClass == nil) then
return
end

local intentString = luajava.newInstance("java.lang.String", "android.intent.action.MAIN")
if (intentString == nil) then
return
end

local categoryString = luajava.newInstance("java.lang.String", "android.intent.category.LAUNCHER")
if (categoryString == nil) then
return
end

local componentNameClass = luajava.bindClass("android.content.ComponentName")
if (componentNameClass == nil) then
return
end

local componentName = luajava.newInstance("android.content.ComponentName", "com.logitech.harmonylink", "com.logitech.harmony.ui.SplashActivity")
if (componentName == nil) then
return
end

local intent = luajava.newInstance("android.content.Intent")
if (intent == nil) then
return
end

intent:setAction(intentString)
intent:addCategory(categoryString)
intent:setComponent(componentName)
intent:setFlags(intentClass.FLAG_ACTIVITY_NEW_TASK)
local na = luajava.nativeActivity()
if (na ~= nil) then
na:startActivity(intent)
end
end

- Matt L
mlove
 
Posts: 22
Joined: Tue Apr 17, 2012 1:26 pm

Re: Launching Android activities from Lua

Postby DarkWishMaster » Wed Nov 13, 2013 8:55 am

why this doesn't work (running on android device)?
local data = {}
local myjava = luajava.newInstance("java.lang.String", "Something here")
data["Mainlayer.mytext.val"] = myjava

gre.set_data(data)
And one more question: does luajava works only when you launch app on android device?
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Launching Android activities from Lua

Postby thomas » Wed Nov 13, 2013 2:46 pm

Did you try explicitly converting the Java String to a string value using tostring()?

To answer your question about luajava availability. It is only available on Android
platforms where there is a Java environment. On other platforms you could certainly
adopt the LuaJava project into your own project, but we don't build it in because
on most platforms Java isn't the system development language.

Thomas
thomas
 
Posts: 132
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Launching Android activities from Lua

Postby thomas » Wed Nov 13, 2013 4:38 pm

I just tried the following and it worked properly:

function set_text(mapargs)
local myjava = luajava.newInstance("java.lang.String", "Something here");

local data = {}
data["Layer1.text_control.text"] = myjava:toString()
gre.set_data(data)
end

Note that you need to call :toString() explicitly on the object otherwise it will be treated as a table (which is what it is if you
call type(myjava) you will see it returns "table")

Thomas
thomas
 
Posts: 132
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Launching Android activities from Lua

Postby DarkWishMaster » Thu Nov 14, 2013 3:14 am

Thanks, really works.
Suppose I have a class "myclass" written in java.
How can I create instance of this class via luajava.newInstance("myclass")
I tried to put myclass.java file in app directory but it doesn't seems to work.
Thank You.
DarkWishMaster
 
Posts: 47
Joined: Wed Oct 23, 2013 6:23 am

Re: Launching Android activities from Lua

Postby thomas » Thu Nov 14, 2013 8:01 am

Unfortunately you can't just take *.java files and include them in a project. They first
need to be compiled by a Java compiler into a Java library (*.jar) file.

Additionally since Android runs a special flavour of Java, those Java libraries need to
be processed.

We are putting a sample together that demonstrates how to make this work, but it
is not yet ready for posting.

Thomas
thomas
 
Posts: 132
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa

Re: Launching Android activities from Lua

Postby thomas » Tue Nov 19, 2013 8:53 am

The sample for loading external Java libraries has been posted in a new message:

viewtopic.php?f=9&t=362

Thomas
thomas
 
Posts: 132
Joined: Mon Nov 02, 2009 4:32 pm
Location: Ottawa


Return to Storyboard for Android

Who is online

Users browsing this forum: No registered users and 1 guest

cron