Search code examples
androidpythonsl4astart-activity

Launch an SL4A script from an SL4A script


I would like to start a background SL4A script (on a remote device) from within a different SL4A script. I can launch a script from a terminal by running something like this:

$ am start -a \
com.googlecode.android_scripting.action.LAUNCH_BACKGROUND_SCRIPT -n \
com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher -e \
com.googlecode.android_scripting.extra.SCRIPT_PATH /sdcard/sl4a/scripts/main.py

I can't translate this into a startActivity call in Python.

The answer to a different question on opening a Twitter client works nicely, but I don't know how to extend that code. For example, how would you add a script path, and where would you put the line com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher?


Solution

  • After many, many failed attempts, I now have this working in Ruby - I had an easier time generating the JSON extras this way than in Python.

    Important! In the command-line version, you call on "com.googlecode.android_scripting/.activity.ScriptingLayerServiceLauncher"

    From within a script, this is called as "com.googlecode.android_scripting.activity.ScriptingLayerServiceLauncher", without the slash. Leaving in the slash crashes sl4a.

    [code]

    require 'android' require 'json/pure'

    d=Android.new

    script = '/sdcard/sl4a/scripts/YOUR_SCRIPT'

    data = {"com.googlecode.android_scripting.extra.SCRIPT_PATH"=>script}

    extras = JSON.generate(data)

    d.startActivity('com.googlecode.android_scripting.action.LAUNCH_BACKGROUND_SCRIPT','','',data,true,'com.googlecode.android_scripting','com.googlecode.android_scripting.activity.ScriptingLayerServiceLauncher')

    [/code]

    I hope this helps!