Java – Custom parameters/variables passed to the Android emulator

Custom parameters/variables passed to the Android emulator… here is a solution to the problem.

Custom parameters/variables passed to the Android emulator

I want to pass a parameter to the android emulator launched via Eclipse. This parameter is a custom parameter that I will use to determine whether the server address to connect to is “localhost” or “myserverdomain.com”. This is because whenever I run a program in a production or local test environment, I don’t want to have two binaries or two versions of the same program.

In pure Java, I can use command-line arguments for this and retrieve them in , or I can also use custom environment variables and main() use System. getProperty().

I can’t find any similar features in Android. Did you know?

Solution

It’s possible, although I haven’t tried to do that with Eclipse yet.

You can use adb on the command line to launch a shell and run the application with parameters.

For example

adb shell am start -a android.intent.action.MAIN -n org.caoilte.MyActivity -e SOME_KEY some_value -e SOME_OTHER_KEY some_other_value

I’ll start my activity with extra content that I can pull from the bundle like this,

public class MyActivity extends Activity {

protected void onStart() {
    super.onStart();


    String someKey = null;
    String someOtherKey = null;

    Bundle extras = getIntent().getExtras();
    if (extras != null) {
        jsEnv = extras.getString("SOME_KEY");
        serverEnv = extras.getString("SOME_OTHER_KEY");
    }
}

Related Problems and Solutions