run SL4A python script from within Android app

Hello folks,
in this post I wanna give you a short introduction how you can launch scripts (specifically python scripts) from your android application.

To follow this tutorial you have to have SL4A installed on your device / emulator.
In case you don’t know SL4A, you can check my previous post on that: https://norwied.wordpress.com/2012/03/28/scripting-on-android/ and I also found another quite good tutorial on how to install SL4A here: https://www.ibm.com/developerworks/opensource/library/mo-python-sl4a-1/index.html

Ok, but now to the interessing part: How to start a python script from within your Android application.

To achieve that I make use of the IntentBuilders.java provided by SL4A, available here:
https://code.google.com/p/android-scripting/source/browse/android/Common/src/com/googlecode/android_scripting/IntentBuilders.java

I also use Constants.java, get it here: https://code.google.com/p/android-scripting/source/browse/android/Common/src/com/googlecode/android_scripting/Constants.java

Now some source code. Fire up eclipse and create a new empty android project. I named mine “AndroidPythonCall” and it automatically creates the “AndroidPythonCallActivity.java”.
Next we have to create a new file called “Constants.java” and pass the content of the file from the “Constants.java” (see link above)
Keep in mind to change the package of the copied data so it matches your package name.
To keep it easy I take only one function from “IntentBuilders.java” and put it directly in my Activity:

/**
 * Builds an intent that launches a script in a terminal.
 *
 * @param script
 *          the script to launch
 * @return the intent that will launch the script
 */
public static Intent buildStartInTerminalIntent(File script) {
  final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
  Intent intent = new Intent();
  intent.setComponent(componentName);
  intent.setAction(Constants.ACTION_LAUNCH_FOREGROUND_SCRIPT);
  intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
  return intent;
}

Another way is to create a second file, name it “IntentBuilders.java” and pass the content of the file mentioned above. In this case you also have to adjust the package name and you have to remove one function:

public static Intent buildInterpreterShortcutIntent(Interpreter interpreter, Parcelable iconResource)

and

import com.googlecode.android_scripting.interpreter.Interpreter;

or you have to provide the Interpreter also, but we don’t want to use it here, so I kicked it 😀

So now we face our Activity again. In the onCreate function we add the following code snippet:

String pythonScript = "/sdcard/sl4a/scripts/hello_world.py";
Intent intent = buildStartInTerminalIntent(new File(pythonScriptPath));
Log.d("SL4A Launcher", "The intent is " + intent.toString());
startActivity(intent);

Also do not forget to copy/paste the function buildStartInTerminalIntent(…) mentioned above to the Activity 😉
Then you can run the program as Android app and you are done. That’s it :D, easy right?

Ok and here, for completeness and in case you had trouble follow my steps, a short warp-up:

  1. create Android project in Eclipse
  2. copy/paste Constants.java to that project
  3. and here is the source required in your Activity:
package org.norbert;

import java.io.*;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class AndroidPythonCallActivity extends Activity {

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // in case you have created a separate IntentBuilders.java file
    // Intent intent = IntentBuilders.buildStartInTerminalIntent(new File("/sdcard/sl4a/scripts/say_time.py"));

    // else use this one
    Intent intent = buildStartInTerminalIntent(new File("/sdcard/sl4a/scripts/hello_world.py"));

    Log.d("SL4A Launcher", "The intent is " + intent.toString());
    startActivity(intent);

  } // onCreate

  /**
  * Builds an intent that launches a script in a terminal.
  *
  * @param script
  *            the script to launch
  * @return the intent that will launch the script
  */
  public static Intent buildStartInTerminalIntent(File script) {
    final ComponentName componentName = Constants.SL4A_SERVICE_LAUNCHER_COMPONENT_NAME;
    Intent intent = new Intent();
    intent.setComponent(componentName);
    intent.setAction(Constants.ACTION_LAUNCH_FOREGROUND_SCRIPT);
    intent.putExtra(Constants.EXTRA_SCRIPT_PATH, script.getAbsolutePath());
    return intent;
  } // buildStartInTerminalIntent
}

I hope this was helpful and you learned something new. I would like to welcome you back in the next post.
Cheers
norbert 😉

Advertisements

3 thoughts on “run SL4A python script from within Android app

  1. Oh my dear friend, nice to read but…. it is beyond my horizon to understand the topic. I like it when you advance. All the best to you and your plans. May all your plans succeed and come true.

  2. Thanks for this wonderful example.
    Dear sir, Kindly let me know, how I can import monkeyrunner package in our python script file.
    I m using from com.android.monkeyrunner import MonkeyRunner, MonkeyDevice.
    But it i not working.
    please help me.
    Thanks and regards Junaid ahmed

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s