Apps How to run ADB command in service?


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.

public void startInterval(){
   ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
   scheduler.scheduleAtFixedRate(new Runnable() {
       public void run() {
           try {
               Log.d("state", "before click");
               Runtime.getRuntime().exec("input tap 100 100");
               Log.d("state", "after click");
           } catch (Exception e){
   }, 0, 1, TimeUnit.SECONDS);

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?


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().