开发者

Custom arguments/variables passed to Android emulator

开发者 https://www.devze.com 2023-03-24 03:30 出处:网络
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\" o

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 an开发者_开发知识库d 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");
    }
}


When you start an emulator to debug your app its like installing on a device. The emulator is not just a mechanism to run your app, it is almost a phone. Creating a command line option to pass data to your app would be a bit complicated as there is no main(String[]) and it would need several options as to who when and how you want the command line options delivered.

Telling a app which server to run against can be accomplished a couple different ways. First you can set it as a String resource. Second you can detect which device you're running on with Build.PRODUCT and other Build values. There is also a few options with using NetworkInterface. Additionally why you're developing your app you could let the user decide with a AlertDialog or ListPreference with debugging as a trigger.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号