Reputation: 323
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 [email protected]"'
Upvotes: 2
Views: 5276
Reputation: 1843
This answer doesn't work for me because -S(force stop) -D(debugging) is not enough to send extra value you should add -n(Component) for sending extra value with -e
here is the sample to sent multiple key-value
adb shell am start -n com.example.jk.checkwifi/.MainActivity -e "imei" $myimei -e "ip" $IP
Upvotes: 2
Reputation: 1007296
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 [email protected]"'
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 [email protected]"
.
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 [email protected]
.
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 [email protected]
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");
Upvotes: 2