I'm looking for way to start my APK with args.
First, How can I get args in OnCreate
method?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get args1, get args2...
}
Then with adb, How can I run my application with those args? I just see example like that:
adb shell am start -S -D com.example.apk/android.app.MainActivity --es args '"-name MYNAME -email test@gmail.com"'
First, How can I get args in OnCreate method?
That is not possible, in the terms that you describe.
Then with adb, How can I run my application with those args?
That is not possible, in the terms that you describe.
What you can do — and what the adb
example shows you — is supply Intent
extras:
adb shell am start -S -D com.example.apk/android.app.MainActivity --es args '"-name MYNAME -email test@gmail.com"'
This is not a particularly good way to use Intent
extras, but let's start with it.
Here, you are passing a string extra named args
with a value of "-name MYNAME -email test@gmail.com"
.
In onCreate()
of your activity, you could call getIntent().getStringExtra("args")
to retrieve that string:
String args = getIntent().getStringExtra("args");
Now, the args
variable will hold -name MYNAME -email test@gmail.com
.
A simpler and cleaner adb shell am start
command would be:
adb shell am start -S -D com.example.apk/android.app.MainActivity --es name MYNAME --es email test@gmail.com
Now you are passing two string extras, name
and email
. You can then retrieve each value individually:
String name = getIntent().getStringExtra("name");
String email = getIntent().getStringExtra("email");