• 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
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?
 
I think if you reach out to @bcrichster he can give some input on how to achieve that goal.

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
 
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/
 
I also now researching theme about accessability services.
its little bit different way, but it also allows to perform clicks beyond the app
 
Back
Top Bottom