14

I need to run my Android app from a remote computer via SSH, but I'm not a command-line expert.

So I would like to know: how to run a specific action (not just open) of an app? Which implies: how to KNOW the exact syntax of possible actions of an app?

Example: I want to start an audio-recording app on my phone from my computer, AND start recording from a remote. Is it possible?

Andrew T.
  • 15,988
  • 10
  • 74
  • 123
Andrea
  • 417
  • 2
  • 5
  • 7

3 Answers3

12

Use this:

am start -a android.intent.action.MAIN -n <package_name>/<full_class_name>

To control an app, you'll have to put correct values of <package_name> and <full_class_name> in the command. For example, you can use com.google.gmail/com.google.gmail.check_mail (Hypothetical names) as last part of command.

Obtaining package name of an app is easy, but obtaining class name of action isn't. There are two problems:

  1. Many app developers keep class info private.

  2. Not all developers do smart modular programming. Its good habit to divide an app in multiple classes which could be triggered by intents, but not all developers are smart.

Solution of 1st problem: Decompile the app using apktool and see all info. There are also other ways, but this one is always-working method (unless app is based on NDK instead of SDK).

Solution of 2nd problem: Nothing.

Don't worry, most of popular apps follow best programming practice and they provide Public API from which you can get class info.

iOS
  • 12,381
  • 13
  • 63
  • 103
  • @Izzy Here's my problem: I'm unable to launch this Activity of Settings app: com.android.settings/.Settings$StorageSettingsActivity. Try it yourself. Actually, what is that string after $ sign ? Is it like a sub-class in the Settings activity? – Gokul NC May 19 '16 at 16:46
  • @GokulNC you might need to quote the entire thing (single quotes), or $StorageSettingsActivity might be seen as a variable and "expanded" to an empty string (unless there's a variable defined by that name). – Izzy May 19 '16 at 20:44
  • Ooh, I didn't even realize that.. Thanks BTW :) – Gokul NC May 20 '16 at 05:07
9

Like Sachin Shekhar said, you must use the following command :

am start -a android.intent.action.MAIN -n <package_name>/<full_class_name>

See a concrete example :

  • getting the apk file from your Android device or any Market places
  • running this command :

aapt dump xmltree com.android.settings*.apk AndroidManifest.xml

I would like to start "tethering" menu, so I search an activity in the output :

(...)
   E: activity (line=190)
    A: android:name(0x01010003)=".TetherSettings" (Raw: ".TetherSettings")
(...)

So the final command is :

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

The aapt command is part of the Android SDK

Gilles Quénot
  • 695
  • 1
  • 5
  • 15
  • Just .TetherSettings in the command wouldn't work. Its not FULL class name.. – iOS Sep 29 '12 at 19:17
  • 2
    No, you can ommit the com.android.settings PATH, that's why I said it's relative. Test it on any app you'd like. – Gilles Quénot Sep 29 '12 at 20:46
  • 2
    I didn't say that it'll not work with other apps. The dot before class name does the magic. But, this shouldn't be encouraged. – iOS Sep 30 '12 at 02:14
  • 1
    You said that's luck, this is not. Do you have sources for your assertion ? Why this shouldn't be encouraged ? – Gilles Quénot Sep 30 '12 at 02:20
  • For an Android developer, its certainly not luck. But, on broad scale, I'd say Android supports relative paths by the luck of user. This shouldn't be encouraged to non-developers due to error prone nature. – iOS Sep 30 '12 at 02:33
0

Use a general purpose logging app (just search logcat on playstore). The logs should show you the app name, class and intent.
Following that run the command in an adb shell or from inside a terminal application in your device:

am start -a <intent> -n <class>
subtleseeker
  • 111
  • 6
  • Could you expand the answer? Suppose I have gotten the app name, class, and intent from the logcat, then what should I do to "run a specific app's action via terminal" (as per the OP's main question)? – Andrew T. Jul 25 '22 at 02:42