Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7671599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T16:05:46+00:00 2026-05-31T16:05:46+00:00

I have an activity that runs some ASCII control over a network port to

  • 0

I have an activity that runs some ASCII control over a network port to a remote device.
Every single button push on the interface will trigger an AsyncTask to handle the communication, and (finally) works great.
However, if a user starts button mashing like a chimp on crack, the system will crash with way too many calls on the same socket, so I’ve come up with a little timer function to slow down the reaction to their excitement.

I’m wondering if somebody has come up with a better way to do this?

First off, inside the onCreate:

btn_pwrtoggle = (Button)findViewById(R.id.pwr_btn);
    btn_pwrtoggle.setOnClickListener(new View.OnClickListener() {           
        @Override
        public void onClick(View v) {
            if(!buttonMasher){
                if(powerstat.equals("OFF")){
                    String[] commandToSend = {"POWER","ON"} 
                }else{
                    String[] commandToSend = {"POWER","OFF"};
                }
                deviceControl(commandToSend);
                }
                startButtonMashTimer();
            }else{
                Log.w("button masher","slow down there, monkey.");
            }
        }
    });

Then, in the actual Activity:

    Timer buttonTimer;
TimerTask buttonMonitorThread;
int chimpCrackCounter;
protected void startButtonMashTimer() {
    chimpCrackCounter = 0;
    buttonTimer = new Timer();
    buttonMonitorThread = new TimerTask(){
        @Override
        public void run(){
            buttonMasher = true;
            if(chimpCrackCounter == 1){
                buttonMasher = false;
                buttonTimer.cancel();
            }
            chimpCrackCounter++;
        }
    };
    buttonTimer.schedule(buttonMonitorThread, 0, 500);
}

It seems to be working just fine, (and may help somebody having the same difficulty) but I’m open to suggestions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T16:05:48+00:00Added an answer on May 31, 2026 at 4:05 pm

    An easy way to prevent a user from pushing a button too often is to save the time when a button was pushed, and then next time compare the last time with the current time and if the difference is too small, ignore the action.

    final static long minTimeBetweenClicks = 1000;
    
    long lastTime;
    onClick(View v){
    if( System.currentTimeMillis() < lastTime + minTimeBetweenClicks ) return;
    //Handle the click
    lastTime = System.currentTimeMillis();
    }
    

    The beauty of this is that it doesn’t require any new threads or timers, and your AsyncTasks won’t have to know about the buttons.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an activity that shows some TextView s and EditText s, as well
I have an activity that some times OutOfMemory exception occurs in some parts of
I have a WPF application that runs in the system-tray & monitors HTTP activity.
I have an activity with some TextViews that I update with current values from
I have an application that runs an activity with a service in a separate
I have an Activity that runs the following code (time and interval are defined):
I have an activity that when users press share on a file it would
I have an Activity that should only get created once. That is, onCreate can
I have an activity that I'd like to load from a preference screen. I'm
I have an activity that opens txt files. And everytime a user wants to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.