Android-GPSd-Forwarder
Android-GPSd-Forwarder copied to clipboard
Possible to start service via ADB / Shell
Hello and happy New Year! I am trying to start the service using a script so it's possible to to use GPSd inside my chroot without interacting with device directly. When running am start-foreground-service --user 0 -n io.github.tiagoshibata.gpsdclient/.GpsdForwarderService
the app immediately produces an ANR. Do you know of a way I can start the service using adb shell or command line?
Here is a snippet from logcat
01-02 20:23:55.183 E/AndroidRuntime(11063): java.lang.RuntimeException: Unable to start service io.github.tiagoshibata.gpsdclient.GpsdForwarderService@78802ee with Intent { cmp=io.github.tiagoshibata.gpsdclient/.GpsdForwarderService }: java.lang.RuntimeException: GpsdClientService requires parameters io.github.tiagoshibata.GPSD_SERVER_ADDRESS and io.github.tiagoshibata.GPSD_SERVER_PORT
The Intent to start the service requires these two parameters (the target server IP address and port). You can check out the Java code here: https://github.com/tiagoshibata/Android-GPSd-Forwarder/blob/fced5fcdc21dacaaf6c67be3f35e59c12691cee8/app/src/main/java/io/github/tiagoshibata/gpsdclient/MainActivity.java#L195-L206
I never tried running it through am
, but I think you have to add -e io.github.tiagoshibata.GPSD_SERVER_ADDRESS [IP address] -e io.github.tiagoshibata.GPSD_SERVER_PORT [port]
. This is the help from am start-*
:
<INTENT> specifications include these flags:
...
[-e|--es <EXTRA_KEY> <EXTRA_STRING_VALUE> ...]
[--esn <EXTRA_KEY> ...]
[--ez <EXTRA_KEY> <EXTRA_BOOLEAN_VALUE> ...]
[-e|--ei <EXTRA_KEY> <EXTRA_INT_VALUE> ...]
...
I tested with Android 7, and the complete command as root user is:
am startservice --user 0 \
-e io.github.tiagoshibata.GPSD_SERVER_ADDRESS 198.51.100.1 \
--ei io.github.tiagoshibata.GPSD_SERVER_PORT 6000 \
io.github.tiagoshibata.gpsdclient/.GpsdForwarderService
The command for newer Androids (IIRC 9 and newer) should use am start-foreground-service --user 0 -n
instead (I haven't tested this command yet):
am start-foreground-service --user 0 -n \
-e io.github.tiagoshibata.GPSD_SERVER_ADDRESS 198.51.100.1 \
--ei io.github.tiagoshibata.GPSD_SERVER_PORT 6000 \
io.github.tiagoshibata.gpsdclient/.GpsdForwarderService
The -e
flag specifies io.github.tiagoshibata.GPSD_SERVER_ADDRESS
as string, while --ei
specifies that io.github.tiagoshibata.GPSD_SERVER_PORT
is an integer.
Thanks for your app :-)
Unfortunately, it is not possible to start the foreground service on Android 11 as is, without root. Fortunately I was able to decompile the .apk and export the service by modifying the manifest. It's also not possible to start it without user interaction of some kind.. i.e. device needs to be unlocked for application to come to front and service to start.
Thank you for all the input, it did not go unoticed!
Here is a small script I have devised for use in Termux with the modified apk.
#!/data/data/com.termux/files/usr/bin/bash
if [[ ${1} == stop ]]; then
adb shell am stop-service --user 0 io.github.tiagoshibata.gpsdclient/.GpsdForwarderService
fi
ADRESS=${2}
PORT=${3}
if [[ -z ${ADDRESS} ]]; then
ADDRESS="127.0.0.1"
fi
if [[ -z ${PORT} ]]; then
PORT="5000"
fi
if [[ ${1} == start ]]; then
SCREEN_STATE=$(adb shell dumpsys power |grep "Display Power:" |cut -d '=' -f2)
until [ ${SCREEN_STATE} = "ON" ]; do
sleep 1
SCREEN_STATE=$(adb shell dumpsys power |grep "Display Power:" |cut -d '=' -f2)
done
adb shell settings put secure location_mode 3
adb shell am start --user 0 io.github.tiagoshibata.gpsdclient/.MainActivity && adb shell am start-foreground-service --user 0 --activity-single-top -D -e io.github.tiagoshibata.GPSD_SERVER_ADDRESS ${ADDRESS} --ei io.github.tiagoshibata.GPSD_SERVER_PORT ${PORT} -n io.github.tiagoshibata.gpsdclient/.GpsdForwarderService && adb shell input keyevent 4
fi
exit 0`