• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps How to run ADB command in service?

leonard31

Lurker
Jun 8, 2017
1
1
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.

Java:
public void startInterval(){
   ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor();
   scheduler.scheduleAtFixedRate(new Runnable() {
       @Override
       public void run() {
           try {
               Log.d("state", "before click");
               Runtime.getRuntime().exec("input tap 100 100");
               Log.d("state", "after click");
           } catch (Exception e){
               System.out.print(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?
 
  • Like
Reactions: sweetndreemy73
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().

https://su.chainfire.eu/
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones