1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to run ADB command in service?

Discussion in 'Android Development' started by leonard31, Jun 8, 2017.

  1. leonard31

    leonard31 Lurker
    Thread Starter

    I wrote simple android service and I would like to execute ADB command in background once per second.

    Function startInterval is executing once in start service method onStartCommand.

    Code (Java):
    1. public void startInterval(){
    2.    ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
    3.    scheduler.scheduleAtFixedRate(new Runnable() {
    4.        @Override
    5.        public void run() {
    6.            try {
    7.                Log.d("state", "before click");
    8.                Runtime.getRuntime().exec("input tap 100 100");
    9.                Log.d("state", "after click");
    10.            } catch (Exception e){
    11.                System.out.print(e);
    12.            }
    13.        }
    14.    }, 0, 1, TimeUnit.SECONDS);
    15. }
    When activity is visible, command is executing and everything works good but when i close activity in service response I get only log message.

    Probably Runtime.getRuntime function works correctly only with activity process running.

    Is there any solutions to execute command in service with closed activity?

    sweetndreemy73 likes this.

    1. Download the Forums for Android™ app!


  2. pro100svitlo

    pro100svitlo Lurker


    Have you found any solution?
    Or any workaround?
  3. KBU2

    KBU2 Android Expert

    I think if you reach out to @bcrichster he can give some input on how to achieve that goal.
    bcrichster likes this.
  4. bcrichster

    bcrichster ROMinator

    Sorry guys, wish I could be more help but I honestly haven't touched many background scripts since playing with init.d in MM 6.0.. Lol. @LV426 might be a better tag for this particular project
  5. pro100svitlo

    pro100svitlo Lurker

    Looks like this "click" command works only at app window, cos as far as I go to phone home screen it stops working...
  6. bcrichster

    bcrichster ROMinator

    Does your device already have a BG app kill enabled?
  7. pro100svitlo

    pro100svitlo Lurker

    no :)
    App still alive, but as far as i know, app window is detached from app window when you hide your app.
  8. bcrichster

    bcrichster ROMinator

    Have you tried other governors?
  9. pro100svitlo

    pro100svitlo Lurker

  10. GameTheory

    GameTheory Android Expert

    I've achieved this in the past by writing an interactive shell. It's called once and remains running until you decide to kill it. Have a look on Stack Overflow, there should be some code samples there to do this.

    You can also use a library to do this. Libsuperuser has an interactive shell in both root and non-root. Have a look at the documentation for libsuperuser. It will teach you a great deal and plenty about Runtime.getRuntime().exec().

  11. pro100svitlo

    pro100svitlo Lurker

    I also now researching theme about accessability services.
    its little bit different way, but it also allows to perform clicks beyond the app

Share This Page