Thursday, January 31, 2008

Run Android Application from Command Line


Sometime, we want to start the android application program from command line (Android Shell). Before, we usually clicking on the icon at application folder to run our program. So, there is an alternative way to run the program.

We use the Android Shell and issue am command to brought up the program. below is an example to do that.
1. Make sure that android emulator is running
2. Enter the shell with issuing command at command shell (prompt): adb shell
3. Issue am command. am command syntax is as follow :

am [start|instrument]
am start [-a <action>] [-d ] [-t <mime_type>]
[-c <category> [-c <category>] ...]
[-e <extra_key> <extra_value> [-e <extra_key> <extra_value> ...]
[-n <component>] [-D] [<uri>]
am instrument [-e <arg_name> <arg_value>] [-p <prof_file>]
[-w] <component>

for example we have android program with Manifest like below:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="com.iftitah.android.contact">
  <application android:icon="@drawable/icon">
   <activity class=".Contact" android:label="@string/app_name">
    <intent-filter>
    <action android:value="android.intent.action.MAIN" />
    <category android:value="android.intent.category.LAUNCHER" />
   </intent-filter>
  </activity>
 </application>
.
.
</manifest>

To run the code issue command like this (in one line):

am start -a android.intent.action.MAIN -n
com.iftitah.android.contact/com.iftitah.android.contact.Contact

ok, Have a nice try!

Do you want to learn more ?
Learning with sample code ?
Learning by Doing ?
Just Visit http://learncodes.googlepages.com/
and there is Android UI Design at there.

8 comments:

Anonymous said...

You forgot the 'start' action:
am start -a android.intent.action.MAIN ...

Bhayangkara said...

Yes, thanks for the correction.
And thanks for reading.

moussam said...

You basically are using an Intent to start an Activity, but from the command line instead of from Java. Any idea how to pass arguments to the activity on the command line?

This is the java equivalent code of what you did from the command line. The commented line is how you would pass an argument to the Intent, I would like to do the equivalent from the command line:
Intent myIntent = new Intent();
myIntent.setClassName("com.iftitah.android.contact","com.iftitah.android.Contact");
//myIntent.putExtra("com.iftitah.android.contact.KEY",VALUE);
this.startActivityForResult(myIntent,1);

moussam said...

Would it be something like this?...

am start -a android.intent.action.MAIN -n
com.iftitah.android.contact/com.iftitah.android.contact.Contact
-e com.iftitah.android.contact.KEY VALUE

salim said...

I have my Android phone connected to the adb shell and I want to be able to run a command that will allow me to open the Browser to browser wepages. I tried the settings and it worked but do not know how do this for the browser app. Here is what worked:

am start -a android.intent.action.MAIN -n com.android.settings/.Settings

Anonymous said...

try this please
"am start -a android.intent.action.MAIN -n com.android.browser/.BrowserActivity"

sp200606 said...
This comment has been removed by the author.
e-turhan said...

hi how can I access Gmail app by this way?