2

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?

Community
  • 1
  • 1
Jeff Darcy
  • 69
  • 8
  • Have you checked this link yet?: http://norwied.wordpress.com/2012/04/11/run-sl4a-python-script-from-within-android-app/ – eazar001 Apr 05 '13 at 03:08
  • I hadn't seen that yet, although some of the code does look familiar - can I import those files "Intent.java" and such into my python scripts, and then do an "Intent i=blah"? – Jeff Darcy Apr 05 '13 at 12:55
  • If you are talking about `IntentBuilders.Java`, SL4A provides you the link to download, all the necessary downloads are provided on that page I gave you. – eazar001 Apr 05 '13 at 21:38
  • The link you sent me talks about writing a java app, and doesn't seem to explain anything related to forming an intent inside sl4a. Sorry, I've read it 4 or 5 times now. – Jeff Darcy Apr 06 '13 at 12:01
  • http://stackoverflow.com/q/18710318/3157779 – Rico Oct 05 '14 at 08:58

2 Answers2

2

This function will launch any SL4A script from inside another one.

The first argument should be a path to the script you want to launch. The script can be in any language you have an interpreter installed for.

The second argument is optional and should be a bool. It defaults to False. It controls whether the terminal will be visible, so you can see output and errors. It does not effect whether the script has a UI or not.

from android import Android
droid = Android()

def launch_script(path, visible=False):

    visibilty   = 'FORE' if visible else 'BACK'
    activity    = 'com.googlecode.android_scripting.action.LAUNCH_{0}GROUND_SCRIPT'.format(visibilty)
    extras      = {'com.googlecode.android_scripting.extra.SCRIPT_PATH': path}
    packagename = 'com.googlecode.android_scripting'
    classname   = 'com.googlecode.android_scripting.activity.ScriptingLayerServiceLauncher'
    intent      = droid.makeIntent(activity, None, None, extras, None, packagename, classname).result

    droid.startActivityIntent(intent)

There's a gist for this code here.

Carl Smith
  • 3,025
  • 24
  • 36
0

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!

Jeff Darcy
  • 69
  • 8
  • The code I posted after the fact is correct, and solved the problem - there is no fail here, except for my lack of familiarity with a language I rarely use.That being said, yone more familiar with Python than I should have no trouble translating the code I posted . – Jeff Darcy Jul 10 '13 at 14:53
  • But it is not an answer to the question, which is how SO works. The question is Python specific and has the Python tag. You should create a new question on how to do it in Ruby and answer your own question there. Other people may have better answers for how to do it in Ruby too. – Carl Smith Feb 17 '15 at 15:32