Android - Starting an activity listed under "Settings" from terminal (or adb shell)
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
- has a
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'
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>