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 ?
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");
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With