Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start android activity from eclipse with custom Intent

I would like, for multiple testing purposes, start my android activity from Eclipse with specific data on the intent (e.g. extra data like a file name to load). Where in the menus can I provide this?

  • In the run configuration, there are nothing in the 3 tabs to provide any arguments
  • I could change some parameters in the resources files but I am afraid I might leak resources which will go to the final application.
  • It is possible to do it in adb: See here but it is not currently associatable with the F11 launch command in Eclipse that is useful for recompiling and relaunching at the same time.
like image 264
Mikaël Mayer Avatar asked Jul 04 '14 07:07

Mikaël Mayer


1 Answers

If your still using eclipse you probably need need to create a simple ant script with a custom task to execute the test. ADB shell has a command to start activities where you can also specify extra's

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>

You would pass them like this:

am start -a android.intent.action.VIEW -c android.intent.category.DEFAULT -e foo bar -e bert ernie -n org.package.name/.MyCustomActivity

P.S. don't forget the dot before the activity.

This can be translated to an ant target which you should put in the ant script.

<target name="run">
    <exec executable="adb">
        <arg value="shell"/>
        <arg value="am"/>
        <arg value="start"/>
        <arg value="-a"/>
        <arg value="android.intent.action.MAIN"/>
        <arg value="-e"/>
        <arg value="extra_key extra_value"/>
        <arg value="-n"/>
        <arg value="{package.name}/{activity}"/>
    </exec>
</target>

which you can execute like this: ant debug install run

How to run ant files from eclipse see:

  • Eclipse Help ant
  • Eclipse Help Application launcher
like image 134
Aegis Avatar answered Sep 22 '22 03:09

Aegis