• Introducing XDA Computing: Discussion zones for Hardware, Software, and more!    Check it out!

How to duplicate what adb does to ad hoc open any given Activity on the Android phone?

Search This thread

GalaxyA325G

Member
May 11, 2021
35
8
I can ad hoc open any given Activity on the Android phone while connected over USB to adb on the Windows PC, but how do I DUPLICATE the SAME THING, but from the phone itself?

For example, how can I run this Activity, ad hoc, on the Android phone?
ACTION: "android.intent.action.MAIN"
PACKAGE: "com.google.android.gms"
CLASS: "com.google.android.gms.ads.settings.AdsSettingsActivity"

For illustrative purposes, below is a trivial example of ad hoc opening the "Reset Advertiser ID" Activity on the Android phone.
1. Install & test adb on your PC (I tested this only on Windows 10)
2. Connect your Android device over USB (mine is Samsung, Android 11)
3. Paste this command into a Windows command window:

C:\> adb shell am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity

That's just a trivial example where I can't yet figure out yet how to duplicate that popping up of a given Activity on the android phone for any given Activity, if all I know is the name of that given Activity (and which isn't already found in a static list inside of the shortcut creator apps).

Here are more examples I've tested for other Activities.
Code:
C:\> adb shell am start -n com.android.settings/.Settings

C:\> adb shell am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity

C:\> adb shell am start -n com.google.android.gms/.location.settings.LocationAccuracyActivity

C:\> adb shell am start -n com.android.settings/.applications.ManageApplications

C:\> adb shell am start -n com.google.android.gms/.update.SystemUpdateActivity

C:\> adb shell am start -n com.samsung.android.secsoundpicker/.SecSoundPickerActivity

C:\> adb shell am start -n com.android.settings/.Settings\$NotificationAppListActivity

C:\> adb shell am start -n com.android.settings/.Settings\$AppMemoryUsageActivity

C:\> adb shell am start -n com.android.settings/.Settings\$NotificationAppListActivity

C:\> adb shell am start -n com.android.settings/.Settings\$SecDisabledAppsActivity

C:\> adb shell am start -n com.android.settings/.Settings\$PowerUsageSummaryActivity

C:\> adb shell am start -n com.android.settings/.Settings\$AppAndNotificationDashboardActivity

C:\> adb shell am start -n com.google.android.gms/.app.settings.GoogleSettingsLink
C:\> adb shell am start -n com.google.android.gms/.app.settings.GoogleSettingsIALink

C:\> adb shell am start -n com.google.android.gms/co.g.Space

C:\> adb shell am start -n com.google.android.gms/.gcm.GcmDiagnostics

C:\> adb shell am start -n com.google.android.gms/.nearby.exposurenotification.settings.SettingsActivity

C:\> adb shell am start -n com.google.android.gms/.nearby.sharing.ContactSelectActivity

C:\> adb shell am start -n com.google.android.gms/.mdm.settings.AdmSettingsActivity

C:\> adb shell am start -n com.android.settings/.network.telephony.MobileNetworkActivity

After each command line above you can close the previous results using the adb command shown below (because you can't open an Android Settings Activity on top of an existing Android Settings Activity):
Code:
C:\> adb shell am force-stop com.android.settings

I'm well aware that we can create permanent homescreen shortcuts
using shortcut creator applications; but this question is not about creating a homescreen shortcut for permanent access to Android Activities.

This question is how to instantly open ANY given Activity ad hoc (i.e., on a case-by-case basis) on Android whenever you want to - just from the name like we did above using adb (but without needing adb to do it).
 
Last edited:

GalaxyA325G

Member
May 11, 2021
35
8
Pack all the ADB commands listed in a Linux shell script ( means omitting "adb shell" part ), put this script into /data/local/tmp, make script executable and finally run this script in Terminal Emulator on phone.
That worked! Thank you!

Below is my first testcase, which was the simple example of popping up the "Reset Advertising ID" Activity on Android when all you know is the unique name of the Activity.

This alias (which I named "resetadid") that worked first was:
$ alias resetadid='am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity'

This saves that alias into your bashrc file for safekeeping.
$ alias >> ~/.bashrc

To run that alias at the Android Termux command line, I just type:
$ resetadid
(which will pop up the named Activity on your phone)

Then I put that line into a shell script that I named "resetadid.sh"
$ am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity

And I put the following line into another shell script (named "closegms.sh") to close that gms (google mobile services) Activity.
$ adb shell am force-stop com.google.android.gms

Note for the shell scripts to work, I had to run these commands.
  • $ pkg install termux-exec
  • $ termux-fix-shebang ./resetadid.sh

This is the "./resetadid.sh" shell script that survives rebooting:
#!/data/data/com.termux/files/usr/bin/bash am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity

To run that script at the Android Termux command line, I just type:
$ ./resetadid.sh
(that will pop up the named Activity on your phone)

Normally we won't need to close that Activity because resetting the advertising ID will close it when we hit the "reset" and "ok" buttons, but for now we need to close the Activity after we pop it up.

To that end is this temporary "closegms.sh" shell script.
#!/data/data/com.termux/files/usr/bin/bash adb shell am force-stop com.google.android.gms

There are only two things left.

1. How to put that Termux command in a homescreen icon for free?
2. What to add to that shell script to actually press the "Reset" button?

Anyone know how to add a shell script to the Android homescreen?
Anyone know how to add a command to press the "reset" button?
 
Last edited:

jwoegerbauer

Senior Member
Jul 11, 2009
7,181
22
1,668
European Union
Typically by long tapping on the home screen, you can create a shortcut to an existing script or add a live folder that contains all of your scripts.

BTW:
Termux is a Terminal Emulator what allows you to run ( Linux conformant ) Android Shell commands / scripts.
 
Last edited:

GalaxyA325G

Member
May 11, 2021
35
8
Typically by long tapping on the home screen, you can create a shortcut to an existing script or add a live folder that contains all of your scripts.
Thanks for that advice which works to create, not a shortcut to a script, but a widget to a script (a shortcut would be nicer as it fits in homescreen folders).

It's not as simple as just long tapping because there is a ton of syntax involved, and the files have to be in critical directories, and even the version of Termux matters extremely greatly.

But it does work. Thanks.

So we can now create a homescreen widget that will bring up any given Activity if all we know is the Activity name!

I'll write it up in the next post.

BTW:
Termux is a Terminal Emulator what allows you to run ( Linux conformant ) Android Shell commands / scripts.

The "right" Termux appears to be that on F-Droid and NOT the one on Google Play as documented elsewhere on the net, this being one link.

For this kind of shell scripts (to run Activities) do you think Andronix will help (Andronis is apparently Linux on top of Android along with Android without the need for rooting).
 
Last edited:

GalaxyA325G

Member
May 11, 2021
35
8
So we can now create a homescreen widget that will bring up any given Activity if all we know is the Activity name!

For anyone who reads this, may I ask that you please invest five minutes in testing this out and letting everyone here know how it works for you?

What I wrote below is designed so you can just follow the cookbook and you should end up with a widget on your homescreen which will open up to ANY named Activity (but I only give one example below) if all you know is the unique name of that Activity.
  1. Install the F-Droid Termux
  2. Install the F-Droid Termux:Widget
  3. Start Termux on your Android device
    • All commands below are run on the Termux command line.
If you had to back out the Google Play Termux in favor of the F-Droid Termux, you'll want to re-create & re-test the alias to a simple Activity such as "Reset Ad ID" as explained earlier in this thread, just to test your syntax.

Re-create:
Code:
alias resetadid='am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity'
Re-test:
Code:
$ resetadid

Then re-create & re-test the shell script we previously described earlier in this thread.

Re-create:
Code:
#!/data/data/com.termux/files/usr/bin/bash
am start -n com.google.android.gms/.ads.settings.AdsSettingsActivity
Re-test:
Code:
$ ./resetadid.sh

Now we're ready to put that command on your homescreen!

Create two directories which are defined in the Termux-Widget help.
Code:
$ mkdir -p $HOME/.shortcuts
$ mkdir -p $HOME/.shortcuts/tasks
Note I'm not sure what the "tasks" directory is for but Termux:Widget docs say to create it.

Move the shell script you created earlier into the $HOME/.shortcuts directory.
Code:
$mv ~/resetadid.sh $HOME/.shortcuts/.

Add the Termux Widget to your homescreen.
Long press your Android homescreen.
Select "Widgets"
Select "Termux:Widget"
Place that "Termux:Widget" on your Android homescreen

Depending on your Android version...
It will ask:
"Create widget and allow access?"
To which you press "Yes" to put your widget on the homescreen.

To interact with the now-running widget, just press the named entry showing up in that Termux Widget.
Code:
resetadid.sh

Depending on your Android version...
It may ask: "Termux requires "Display over other apps" permission
to start terminal sessions from background on Android >=10."
"Grants it from Settings -> Apps -> Termux -> Advanced" [sic]

If needed, grant Termux permission to display over other apps:
Code:
Android11:Settings > Apps > Your apps > Termux > Appear on top = (change off to on)

Now you can press the always running Termux:Widget icon on your homescreen to bring up the desired Activity.

Does this work for you to bring up the named Activity on Android with the only thing you know being the unique Activity name?

If so, here's what's left that I know of:
a. Figure out how to add a step to actually press the "Reset" button!
b. Figure out how to use a shortcut instead of an always-running widget (which can't be placed inside a homescreen folder)
c. Figure out how to run this automatically such as when there is a screen unlocking event.

--
Notes: Keep in mind the goal is to be able to interact with ANY known Android Activity using only freeware (so that everyone can do it); resetting the ad id is just one of the simplest examples.

Interested users can try this on the "Show Running Services" Activity (e.g., for implementing a "ps -aux|kill -9" shortcut).
 
Last edited:

Top Liked Posts

  • There are no posts matching your filters.
  • 1
    Pack all the ADB commands listed in a Linux shell script ( means omitting "adb shell" part ), put this script into /data/local/tmp, make script executable and finally run this script in Terminal Emulator on phone.