Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom arguments/variables passed to Android emulator

I'd like to pass an argument to the android emulator launched via Eclipse. This argument is a custom one that I would use to determine if the server's address to connect to is either "localhost" or "myserverdomain.com". This is because I don't want to have two binaries, or two versions, of the same program, whenever I run the program in production or in local test environment.

In plain Java, I can use the command line arguments for that matter, and retrieve them in the main(), or also use the custom environment variables and retrieve them with System.getProperty().

I can't find any similar feature in Android. Do you know any please ?

like image 321
Joel Avatar asked Feb 23 '23 09:02

Joel


1 Answers

This is possible, although I haven't tried to do it via Eclipse.

From the command-line you can use adb to launch a shell and run an 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

will start my activity with extras that I can extract from the bundle like so,

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");
    }
}
like image 83
Caoilte Avatar answered Mar 08 '23 07:03

Caoilte