user2921569
user2921569

Reputation: 1

Autoclick in Android

I'm developing an Android application in which I have different buttons.

In particular there is one that, in addition to the user whenever pressed, I want "autoclick" every X seconds.

Upvotes: 0

Views: 2315

Answers (1)

yilmazburk
yilmazburk

Reputation: 917

You can use ScheduledExecutorService to create timer and autoclicker like that.

private void yourFunction(){
   //whatever you want
}
yourButton.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
    yourFunction();
  }
});

ScheduledExecutorService scheduleTaskExecutor= Executors.newScheduledThreadPool(1);
  scheduleTaskExecutor.scheduleAtFixedRate(new Runnable() {
    public void run() {
      yourFunction();
  }
}, 0, YourSeconds, TimeUnit.SECONDS);

and you should close ScheduledExecutorService in your activity's onDestroy method like that.

public void onDestroy() {
  super.onDestroy();
  if (scheduleTaskExecutor != null)
    scheduleTaskExecutor.shutdownNow();
}

Upvotes: 2

Related Questions