13

I would like to start this activity from terminal: com.android.settings.Settings$PowerUsageSummaryActivity

I tried

am start -S com.android.settings/.Settings$PowerUsageSummaryActivity

and this is what I get:

Stopping: com.android.settings
Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=com.android.settings/.Settings }

The problem is am start command omits the $PowerUsageSummaryActivity part, so basically I'm just getting standard "Settings" menu open. how can I open that specific "power usage summary" page from terminal?

2
  • 2
    Have you tried putting it into single quotes, i.e. 'com.android.settings/.Settings$PowerUsageSummaryActivity' -- or escaping the $ like com.android.settings/.Settings\$PowerUsageSummaryActivity -- which is the usual stuff to do on the shell when dealing with literal $ signs not indicating a variable?
    – Izzy
    Commented May 8, 2013 at 14:20
  • How about open Tasker's RunLog activity on top from terminal? Tasker has a 3 dot menu to open the Log window.
    – Nicola Al.
    Commented Feb 17, 2017 at 13:05

2 Answers 2

12

As I wrote in my comment, there are some special characters needing extra care when working at the shell prompt (or in shell scripts). One of them is the $ sign, which usually indicates a variable. If that should be taken literally, you need to escape it (or enclose the entire string by single quotes). Similar rules for quotation marks.

How your command should look like with an escaped $, you can already find in eldarerathis' answer:

shell@android:/ # am start -n com.android.settings/.Settings\$PowerUsageSummaryActivity

Note the "back-slash" in front of the $ -- that's the escape sign. Use the same for quotation marks or blanks, if your command includes some to be taken literally, e.g.

myscript.sh first\ parameter\!
myscript.sh "first parameter!"

both would do the same: Making the string a single parameter. In the example of your am start command, this is what happened on parsing:

  • command: am
  • parameter 1: start
  • parameter 2: -S
  • parameter 3: com.android.settings/.Settings$PowerUsageSummaryActivity
    • has a $, interpreting: variable $PowerUsageSummaryActivity is not set, so empty
    • conclusion: parameter 3 is com.android.settings/.Settings

Note also that if you run this directly via adb shell, the command goes through shell parsing twice, so you need to escape or quote the command again, like this:

user@desktop:~$ adb shell am start -n 'com.android.settings/.Settings\$PowerUsageSummaryActivity'
5
  • oh thanks! oh how could i forget to escape $... i'm duly shamed :)
    – juniecho
    Commented May 8, 2013 at 14:38
  • The little things in life... Easy to overlook :) Btw: If that solved your issue, consider marking one of the answers as "accepted" (clicking the checkmark next to it) [and upvote the other :)] -- so it's clear to other people tapping into a similar trap :)
    – Izzy
    Commented May 8, 2013 at 14:41
  • oh i found the checkmark. so this is how i accept the answer... thanks this is the first time i use stackexchange ;)
    – juniecho
    Commented May 9, 2013 at 2:21
  • You're welcome anytime :) Now you see how well it works here: spread the word :)
    – Izzy
    Commented May 9, 2013 at 10:45
  • 3
    With bash, in my case, only this worked: adb shell 'am start -n com.android.settings/.Settings\$PowerUsageSummaryActivity'. Don't know why.
    – Firelord
    Commented Jan 8, 2016 at 23:24
6

Escape the $ in the sub-class name and it should work:

shell@android:/ # am start -S com.android.settings/.Settings\$PowerUsageSummaryActivity
Starting: Intent { cmp=com.android.settings/.Settings$PowerUsageSummaryActivity }
shell@android:/ #

Another option is to instead send the intent that the Power Usage screen listens for:

shell@android:/ # am start -a android.intent.action.POWER_USAGE_SUMMARY

You can find the intents by looking at the <action> tags in the AndroidManifest.xml file for the Settings "application" (which can be viewed on GitHub). As an example, here is the activity definition for the Settings$PowerUsageSummaryActivity:

<activity android:name="Settings$PowerUsageSummaryActivity"
        android:label="@string/power_usage_summary_title"
        android:uiOptions="none"
        android:taskAffinity=""
        android:excludeFromRecents="true">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <action android:name="android.intent.action.POWER_USAGE_SUMMARY" />
        <category android:name="android.intent.category.DEFAULT" />
        <category android:name="com.android.settings.SHORTCUT" />
    </intent-filter>
    <!-- Some other stuff here... -->
</activity>

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .