Java code to take device screen cap - Android Q&A, Help & Troubleshooting

Hi All,
In my Java program I would like to capture screen of android device attached through adb. For that in command prompt I am able to execute
"adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > Screen.png"
How to do this in my program. I wrote code that create process and execute "adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g'". but I am unable to read input stream and write it to a .jpg file. File is corrupted and cannot open,
Thank you

iua said:
Hi All,
In my Java program I would like to capture screen of android device attached through adb. For that in command prompt I am able to execute
"adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g' > Screen.png"
How to do this in my program. I wrote code that create process and execute "adb shell screencap -p | perl -pe 's/\x0D\x0A/\x0A/g'". but I am unable to read input stream and write it to a .jpg file. File is corrupted and cannot open,
Thank you
Click to expand...
Click to collapse
Are you talking about Android Java or regular Java?
Have you tested using the command separately and tested what the output is?
BTW. Your perl line is wrong!, This is the good one
Code:
adb shell screencap -p | sed 's/\r$//' > screen.png
the one you tried is only for MAC OSX

broodplank1337 said:
Are you talking about Android Java or regular Java?
Have you tested using the command separately and tested what the output is?
BTW. Your perl line is wrong!, This is the good one
Code:
adb shell screencap -p | sed 's/\r$//' > screen.png
the one you tried is only for MAC OSX
Click to expand...
Click to collapse
Hi, I am talking about regular java. My java application is a desktop app run on PC. Yes I tested command in consol, it nicely captures screen of device attached via adb and save as a image files as per command given.
Now my requirement is to write a java program run on PC to capture screen, issue is I don't have idea how to do it with that way. If explain further I hope I could use "Runtime.exec" to pass "adb shell screencap -p | sed 's/\r$//' " but how can I create a image file on Workstation.

Related

[Q] busybox dd command not working when used in C# - please help

Right, I'm currently working on a project which involves imaging Android phones - specifically, the program I've written is mean to automate identifying the memory block containing the userdata and then using the following sequence of commands (using Android Debug Bridge) to copy it to the computer that the phone is connected to:
Code:
adb forward tcp:5555 tcp:5555
adb shell
busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number] bs=4096
This is done in command prompt and then you have to manually open another command prompt and type in:
Code:
adb forward tcp:5555 tcp:5555
nc 127.0.0.1 5555 | dd of=[chosen file name].bin bs=4096 -- progress
This copies the memory block over without any problems when done manually and even shows you in real time how much data has been transferred.
The problem I have is with automating the process in C#. Specifically, I can get every stage of it to work through using a process to write the commands to standard input and using a tcp listening port to receive the data. Apart from this crucial element of the process:
Code:
adb shell "busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number] bs=4096"
For some reason, no matter how I try to phrase it or do it, C# hates that command and won't execute it. I can manually type the exact same thing into the command prompt and it works fine but trying to do it in C# just leads to busybox acting as though I typed the command in incorrectly - this is what it gives me:
Code:
BusyBox v1.15.2 <2009-11-27 10:38:30 GMT> multi-call binary
Usage: nc [-in] [-wN] [-l] [-p Port] [-f FILENAME|PADDR PORT] [-e PROG]
Open a pipe to IP:port or file
Options:
-e PROG Run prog after connect
-i SEC Delay interval for lines sent
-w SEC Timeout for connect
-f FILE Use file <ala /dev/ttyS0> instead of network
-l Listen mode, for inbound connects
<use -l twice with -e for persistent server>
-p PORT Local port
The problem appears to be particularly with the use of bs=[bytes] but I've no idea why - and I've spent several hours searching all over the web for solutions.
So basically, I was wondering whether anyone else might have encountered this issue before and if anyone has any ideas to get around it? At the moment I'm using a script to send the commands to the command prompt but that's a kludge that I'd really prefer not to have to use.
Thanks in advance for any suggestions or comments and apologies if I've posted this in the wrong place.
Probably has to do with syntax.
In the first instance you don't have it in quotes.
Code:
busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number] bs=4096
Then you do have quotes the second time.
Code:
adb shell "busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number] bs=4096"
Sometimes, using a newer busybox helps too... 1.15.2 is kinda old
The first time is meant to show that it's being done on two different lines though I probably should have made that clearer.
Thanks for the suggestion - I just upgraded to the latest version of busybox and also altered my code so it would open cmd.exe and then run adb.exe rather than skipping straight to running adb.exe. This seems to have fixed the problem some of the time so there's probably a timing issue as well.
So I don't know whether it was the busybox version or the adb.exe/cmd.exe thing but, either way, the problem seems to have disappeared so I'm not going to change anything else in case I break it again.
Thanks very much for your help
Yep I find that there is always 2 or 3 ways to do something and usually only 1 way works all the time (lowest common denominator etc...)
@Antonine May I ask you what's the project you're talking about? I'm interested...

[Q] How to pipeline pipe contents from android phone via adb to my computer?

Suppose that I do the following command from windows:
Code:
adb shell cat /sdcard/myfile.zip > c:\file.zip
The result is that the two files don't match, even they have much different file size. Using 'adb pull' works but is not compatible with pipelining or fifos.
Any help?
EDITED: here is the solution:
Antonine said:
Right, I'm currently working on a project which involves imaging Android phones - specifically, the program I've written is mean to automate identifying the memory block containing the userdata and then using the following sequence of commands (using Android Debug Bridge) to copy it to the computer that the phone is connected to:
Code:
adb forward tcp:5555 tcp:5555
adb shell
busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number]
This is done in command prompt and then you have to manually open another command prompt and type in:
Code:
adb forward tcp:5555 tcp:5555
nc 127.0.0.1 5555 | dd of=[chosen file name].bin -- progress
This copies the memory block over without any problems when done manually and even shows you in real time how much data has been transferred.
The problem I have is with automating the process in C#. Specifically, I can get every stage of it to work through using a process to write the commands to standard input and using a tcp listening port to receive the data. Apart from this crucial element of the process:
Code:
adb shell "busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number]"
For some reason, no matter how I try to phrase it or do it, C# hates that command and won't execute it. I can manually type the exact same thing into the command prompt and it works fine but trying to do it in C# just leads to busybox acting as though I typed the command in incorrectly - this is what it gives me:
Code:
BusyBox v1.15.2 <2009-11-27 10:38:30 GMT> multi-call binary
Usage: nc [-in] [-wN] [-l] [-p Port] [-f FILENAME|PADDR PORT] [-e PROG]
Open a pipe to IP:port or file
Options:
-e PROG Run prog after connect
-i SEC Delay interval for lines sent
-w SEC Timeout for connect
-f FILE Use file <ala /dev/ttyS0> instead of network
-l Listen mode, for inbound connects
<use -l twice with -e for persistent server>
-p PORT Local port
The problem appears to be particularly with the use of bs=[bytes] but I've no idea why - and I've spent several hours searching all over the web for solutions.
So basically, I was wondering whether anyone else might have encountered this issue before and if anyone has any ideas to get around it? At the moment I'm using a script to send the commands to the command prompt but that's a kludge that I'd really prefer not to have to use.
Thanks in advance for any suggestions or comments and apologies if I've posted this in the wrong place.
Click to expand...
Click to collapse
zeppelinrox said:
Probably has to do with syntax.
In the first instance you don't have it in quotes.
Code:
busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number]
Then you do have quotes the second time.
Code:
adb shell "busybox nc –l –p 5555 –e dd if=/dev/mtd/mtd[mtd block number]"
Sometimes, using a newer busybox helps too... 1.15.2 is kinda old
Click to expand...
Click to collapse
NOTE: you have to use unix paths with cygwin, not DOS ones. Use cygpath to convert:
Code:
cygpath -u "C:\your path\folder\archive"
Thanks man, you saved my life! I just deleted some pictures from my Nexus 7 and I had to find a way to dump the internal storage to my computer in order to use it with PhoroRec...
scandiun said:
Suppose that I do the following command from windows:
Code:
adb shell cat /sdcard/myfile.zip > c:\file.zip
The result is that the two files don't match, even they have much different file size. Using 'adb pull' works but is not compatible with pipelining or fifos.
Any help?
EDITED: here is the solution:
Click to expand...
Click to collapse
can't seem to get the -- progress part to work it kicks back error of invalid command
Code:
" adb forward tcp:5555 tcp:5555 "
" adb shell "
" su "
" busybox nc 127.0.0.1 5555 | dd of=C:\Users\PB-2\Desktop\GN-3_MJ5_Full.raw.img bs=4096 "
After inputting the last line I get:
: cannot open for write: Read-only file system
any Ideas? I don't understand how my pc desktop could be read only
Thanks in Advance
Phatboyj420 said:
can't seem to get the -- progress part to work it kicks back error of invalid command
Code:
" adb forward tcp:5555 tcp:5555 "
" adb shell "
" su "
" busybox nc 127.0.0.1 5555 | dd of=C:\Users\PB-2\Desktop\GN-3_MJ5_Full.raw.img bs=4096 "
After inputting the last line I get:
: cannot open for write: Read-only file system
any Ideas? I don't understand how my pc desktop could be read only
Thanks in Advance
Click to expand...
Click to collapse
You can't use DOS paths on cygwin, you have to use unix ones:
Code:
busybox nc 127.0.0.1 5555 | dd of=/cygdrive/c/Users/PB-2/Desktop/GN-3_MJ5_Full.raw.img bs=4096
You can convert any DOS path to unix with cygpath if you don't figure out directly (even on other drives):
Code:
cygpath -u "C:\Users\PB-2\Desktop\GN-3_MJ5_Full.raw.img"
Result:
Code:
/cygdrive/c/Users/PB-2/Desktop/GN-3_MJ5_Full.raw.img
scandiun said:
You can't use DOS paths on cygwin, you have to use unix ones:
Code:
busybox nc 127.0.0.1 5555 | dd of=/cygdrive/c/Users/PB-2/Desktop/GN-3_MJ5_Full.raw.img bs=4096
You can convert any DOS path to unix with cygpath if you don't figure out directly (even on other drives):
Code:
cygpath -u "C:\Users\PB-2\Desktop\GN-3_MJ5_Full.raw.img"
Result:
Code:
/cygdrive/c/Users/PB-2/Desktop/GN-3_MJ5_Full.raw.img
Click to expand...
Click to collapse
Awesome Thanks,
I'm assuming this is true in both cd & dd via cygwin?
I was originally trying to input the commands in DOS because your OP doesn't specify, or say to use cygwin.
I came to the realization of using cygwin after reading other guides.
I've been looking at some of your other guides I owe You many thanks as a Result
Phatboyj420 said:
Awesome Thanks,
I'm assuming this is true in both cd & dd via cygwin?
I was originally trying to input the commands in DOS because your OP doesn't specify, or say to use cygwin.
I came to the realization of using cygwin after reading other guides.
I've been looking at some of your other guides I owe You many thanks as a Result
Click to expand...
Click to collapse
Yes, since cygwin is to be compatible with unix scripts paths must be always in unix format.
You can use dos paths directly
Code:
cd `cygpath -u "C:\your path\folder\archive"`
I've updated the first post
scandiun said:
Yes, since cygwin is to be compatible with unix scripts paths must be always in unix format.
You can use dos paths directly
Code:
cd `cygpath -u "C:\your path\folder\archive"`
I've updated the first post
Click to expand...
Click to collapse
Awesome share Thanks
I still can't get this to work "for the life of me" I know, I now have cygwin setup right, as it works with your example here
Phatboyj420 said:
Awesome share Thanks
I still can't get this to work "for the life of me" I know, I now have cygwin setup right, as it works with your example here
Click to expand...
Click to collapse
Are you still getting "cannot open for write"?
scandiun said:
Are you still getting "cannot open for write"?
Click to expand...
Click to collapse
Nope you set me strait on that one
In the first window I do
Code:
" adb forward tcp:5555 tcp:5555 "
" adb shell "
" su "
" system/xbin/busybox nc –l –p 5555 –e dd if=/dev/block/mmcblk0 bs=4096 "
and get the usage response
Code:
BusyBox v1.22.1-Stericson (2014-01-25 17:27:18 CET) multi-call binary.
Usage: nc [-iN] [-wN] [-l] [-p PORT] [-f FILE|IPADDR PORT] [-e PROG]
Open a pipe to IP:PORT or FILE
-l Listen mode, for inbound connects
(use -ll with -e for persistent server)
-p PORT Local port
-w SEC Connect timeout
-i SEC Delay interval for lines sent
-f FILE Use file (ala /dev/ttyS0) instead of network
-e PROG Run PROG after connect
If I trade out the string from your other example here, " The only problem i'm having with your other example is that I can't specify the " bs=4096 " "
I can get through to the second window
Example
Code:
[email protected] ~
$ adb forward tcp:5555 tcp:5555
[email protected] ~
$ adb shell
[email protected]:/ $ su
su
[email protected]:/ # /system/xbin/busybox nc -l -p 5555 -e /system/xbin/busybox dd if=/dev/block/mmcblk0 bs=4096
555 -e /system/xbin/busybox dd if=/dev/block/mmcblk0 bs=4096 <
The second terminal then gives me an error at the last string
Output of second terminal
Code:
[email protected] ~
$ adb forward tcp:5555 tcp:5555
[email protected] ~
$ nc 127.0.0.1 5555 | dd of=/cygdrive/c/Users/PB-2/Desktop/GN-3_Tmp/mmcblk0.raw bs=4096 -- progress
dd: unrecognized operand `progress'
Try `dd --help' for more information.
If I run it with out, " -- progress " it runs but with-out the desired effect of the progress response
Code:
[email protected] ~
$ adb forward tcp:5555 tcp:5555
[email protected] ~
$ nc 127.0.0.1 5555 | dd of=/cygdrive/c/Users/PB-2/Desktop/GN-3_Tmp/mmcblk0.raw bs=4096
I'm at a loss I need to figure out how to run the pv and the dd of= in the same line i guess
or to some how combine
Code:
" nc 127.0.0.1 5555 | dd of=/cygdrive/c/Users/PB-2/Desktop/GN-3_Tmp/mmcblk0.raw bs=4096 "
&
Code:
" nc 127.0.0.1 5555 | pv -i 0.5 > /cygdrive/c/Users/PB-2/Desktop/GN-3_Tmp/mmcblk0.raw "
So as to achieve both the bs= & the Read-out notification
Thanks tremendously, for your assistance thus far.
That's right, some android implementations of dd lack advanced parameters. You can try either installing Busybox from Stericson or just skip the bs option.
Removed all the "bs=4096" from quotations in first post, since it's not needed at all.
I keep getting
write error: File too large, when trying to dump my /data partition to my external sd card.
The external card is exFat formatted, which is supposed to be able to handle larger than 4Gig files
here's my command
Code:
busybox dd if/dev/block/mmcblk0p29 of=/mnt/extSdCard/data.ext4.img
any ideas?
p.s. yes, I do have more than 4G's of software on my phone
kevp75 said:
I keep getting
write error: File too large, when trying to dump my /data partition to my external sd card.
The external card is exFat formatted, which is supposed to be able to handle larger than 4Gig files
here's my command
Code:
busybox dd if/dev/block/mmcblk0p29 of=/mnt/extSdCard/data.ext4.img
any ideas?
p.s. yes, I do have more than 4G's of software on my phone
Click to expand...
Click to collapse
Two ideas:
1 - Try from recovery
2 - If that doesn't work put the sd into a card reader and connect it as otg drive.
scandiun said:
Two ideas:
1 - Try from recovery
2 - If that doesn't work put the sd into a card reader and connect it as otg drive.
Click to expand...
Click to collapse
I can try from recovery sure, but how would #2 solve the issue? (don't know what otg drive means)
kevp75 said:
I can try from recovery sure, but how would #2 solve the issue? (don't know what otg drive means)
Click to expand...
Click to collapse
Search google images for otg
scandiun said:
Search google images for otg
Click to expand...
Click to collapse
didnt work. I get the same issue
Rockin' it from my Smartly GoldenEye 35 NF1 (muchas gracias:* @iB4STiD @loganfarrell @muniz_ri @Venom0642 @ted77usa @rebel1699* @iB4STiD) ~ 20GB free cloud https://copy.com?r=vtiraF
Check me out online @ http://kevin.pirnie.us
kevp75 said:
didnt work. I get the same issue
Click to expand...
Click to collapse
Format a pendrive in NTFS or HFS+ and connect it to your phone via OTG, if it's not detected directly there are some apps on play store.
scandiun said:
Format a pendrive in NTFS or HFS+ and connect it to your phone via OTG, if it's not detected directly there are some apps on play store.
Click to expand...
Click to collapse
NTFS worked. My data partition is over 8Gigs! But I was under the impression that exFat was able to support that... but I guess not...
kevp75 said:
NTFS worked. My data partition is over 8Gigs! But I was under the impression that exFat was able to support that... but I guess not...
Click to expand...
Click to collapse
Next time you can try copying some big data to the exfat from the computer itself, just to see if prompts some error.
scandiun said:
Next time you can try copying some big data to the exfat from the computer itself, just to see if prompts some error.
Click to expand...
Click to collapse
tried that too had a 9.7g dvd imaage i tried with and it failed at the 4g mark. ext4 should be the standard
Rockin' it from my Smartly GoldenEye 35 NF1 (muchas gracias:* @iB4STiD @loganfarrell @muniz_ri @Venom0642 @ted77usa @rebel1699* @iB4STiD) ~ 20GB free cloud https://copy.com?r=vtiraF
Check me out online @ http://kevin.pirnie.us

[Q] How to use monkey tool for stress testing

I have been testing an android application in the device using the monkey tool for stress testing.
I have referred to the link developer.android.com/tools/help/monkey.htm(This is URL). After executing the command ($ adb shell monkey -p your.package.name -v 500) in the terminal emulator from the device i can see the status as killed your.package.name (my package name).
Is the command executed successfully? or not!
is there any way i can log the errors or results in the device?
help is appreciated
Thank you

[Q] A command to change directory in ubuntu

Hi, if possible how can I change directory in a terminal window in ubuntu using a short command such as 'croot' instead of using 'cd ~/android/system' ??
wassupasian said:
Hi, if possible how can I change directory in a terminal window in ubuntu using a short command such as 'croot' instead of using 'cd ~/android/system' ??
Click to expand...
Click to collapse
not sure what "croot" is? You mean "chroot root:root /to/destination?" "cd ~/ take your logins home folder. You can always cd / for root or cd /etc /bin /sbin or whatever root subfolder from anywhere. but i am unaware of any true short cuts.
hope that helps,
dfl
If the bash stuff is equal to Ubuntu Desktop systems you can do something like this:
in terminal:
Code:
$ echo "alias croot='cd ~/android/system'" >> $HOME/.bash_aliases
logout -> login (reboot i guess)

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

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"
Click to expand...
Click to collapse
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).
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.
jwoegerbauer said:
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.
Click to expand...
Click to collapse
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
Click to expand...
Click to collapse
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?
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.
jwoegerbauer said:
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.
Click to expand...
Click to collapse
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.
jwoegerbauer said:
BTW:
Termux is a Terminal Emulator what allows you to run ( Linux conformant ) Android Shell commands / scripts.
Click to expand...
Click to collapse
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).
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.
Install the F-Droid Termux
(Do not use the Google Play Termux!)
Install the F-Droid Termux:Widget
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
Click to expand...
Click to collapse
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.
Click to expand...
Click to collapse
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]
Click to expand...
Click to collapse
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).

Categories

Resources