Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Possible to start service via ADB / Shell #17

Open
Geofferey opened this issue Jan 3, 2023 · 3 comments
Open

Possible to start service via ADB / Shell #17

Geofferey opened this issue Jan 3, 2023 · 3 comments

Comments

@Geofferey
Copy link

Geofferey commented Jan 3, 2023

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

@tiagoshibata
Copy link
Owner

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:

Intent intent = new Intent(activity, GpsdForwarderService.class);
intent.putExtra(GpsdForwarderService.GPSD_SERVER_ADDRESS, address)
.putExtra(GpsdForwarderService.GPSD_SERVER_PORT, port);
activity.print("Streaming to " + address + ":" + port);
try {
if (!activity.bindService(intent, activity.serviceConnection, BIND_ABOVE_CLIENT | BIND_IMPORTANT)) {
throw new RuntimeException("Failed to bind to service");
}
if (activity.startService(intent) == null) {
activity.unbindService(activity.serviceConnection);
throw new RuntimeException("Failed to start service");
}

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> ...]
    ...

@Enrico204
Copy link

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 :-)

@Geofferey
Copy link
Author

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`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants