Storyboard Lua Android Example

This example demonstrates how a phone call could be invoked as part of a Lua callback. In order for this example to work, the AndroidManifest.xml file must be changed to give permission for calls to be made: %<uses-permission android:name="android.permission.CALL_PHONE"></uses-permission>

 -- Log message routine to route diagnostic messages
local function lm(msg)
    print(msg)
end

-- Call a selected phone number using the Android API
-- Input is the string number value that is to be called
local function call_phone_number(number)
    if(luajava == nil) then
        lm("No luajava Lua object")
        return
    end

    local na = luajava.nativeActivity()
    if(na == nil) then
        lm("No native activity available")
        return
    end

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

    local phoneURI = uriClass:parse("tel:" .. tostring(number))
    if(phoneURI == nil) then
        lm("No java.net.URI object")
        return
    end

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

    local intent = luajava.newInstance("android.content.Intent",
    intentClass.ACTION_CALL, phoneURI)
    if(intent == nil) then
        lm("No intent object")
        return
    end

    lm("Calling " .. number)
    na:startActivity(intent)
end

                        

This example demonstrates how to create a new instance of a nested inner class of a Java class. This example gets media metadata from the android.provider.MediaStore.Audio.Media class, which is a nested class of android.provider.MediaStore.Audio, which in turn is a nested class of android.provider.MediaStore.

-- In order to pass array's to any of the Android Java API's we must explicitly create
-- a Java array from a Lua table and this function covers that work.
function make_array(dataClass, values)
    local arrayClass = luajava.bindClass("java.lang.reflect.Array")
    if(arrayClass == nil) then
        print("Can't get array class")
        return nil
    end

    local newTypedArray = arrayClass:newInstance(dataClass, #values)
    if(newTypedArray == nil) then
        print("Can't get array class")
        return nil
    end

    for i=1,#values do
        arrayClass:set(newTypedArray, i-1, values[i])
    end

    return newTypedArray
end

function get_album_files(album_id)
    if (luajava == nil) then
        return
    end

    if (luajava.bindClass == nil) then
        return
    end
    local na = luajava.nativeActivity()

    local mediastore = luajava.newInstance("android.provider.
    MediaStore$Audio$Media")
    local externalURI = mediastore.EXTERNAL_CONTENT_URI
    local columns = {}
    columns[1] = mediastore.TITLE_KEY
    columns[2] = mediastore.DURATION
    columns[3] = mediastore.TITLE

    local stringClass = luajava.bindClass("java.lang.String")
    local array = make_array(stringClass, columns)
    local where = mediastore.ALBUM_KEY .. "=?"
    local what = {}
    what[1] = album_id
    local whatArray = make_array(stringClass, what)

    local cursor = na:managedQuery(externalURI, array, where, whatArray, nil);
    local res = cursor_to_table(cursor)
    return res
end
Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments

0 comments

Article is closed for comments.