Start Android activity from command line with extra

25,600

The right command should be

adb shell am start -n com.example.mike.app/.SimpleActivity --es "Message" "hello!"

with -n....

Share:
25,600
Mike Lee
Author by

Mike Lee

Updated on November 17, 2020

Comments

  • Mike Lee
    Mike Lee over 3 years

    I created a simple activity that I want to start from command line and pass in some value from command line.

    However, if I try to do

    adb shell am start com.example.mike.app/.SimpleActivity --es "Message" "hello!"
    

    and then receive the message in activity, intent.getExtras() returns null.

    Activity:

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.simple_activity);
    
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        Log.d(LOGTAG, intent == null ? "Intent is null" : "Intent is not null");
        Log.d(LOGTAG, bundle == null ? "Bundle is null" : "Bundle is not null");
    }
    

    Result:

    SimpleActivity(12345): Intent is not null
    SimpleActivity(12345): Bundle is null
    
  • Ubaier Bhat
    Ubaier Bhat over 6 years
    How can we pass then extra of type long?
  • Sodino
    Sodino over 6 years
    @UbaierBhat [--el <EXTRA_KEY> <EXTRA_LONG_VALUE> ...]
  • Weekend
    Weekend about 6 years
    There is the docs of am start. Or, adb shell am help start to view the full args list.