Java – spam protection onclick()

spam protection onclick()… here is a solution to the problem.

spam protection onclick()

I’m trying to build an SMS app that sends SMS at the push of a button, this part works fine, but now I’m trying to implement spam protection.
Spam protection means you can only send 1 SMS every 10 seconds (or more).

I’ve tried it :

sentSMS.setOnClickListener(new OnClickListener() {
   public void onClick(View v) {

Timer timer = new Timer();

int seconds = 10000;
       timer.schedule(new TimerTask() {
           public void run() {
               processClick();
           }
       }, seconds);

}});

But when I pressed the button twice, it didn’t work, and the text message was sent twice.

Maybe making a toast with how many seconds the user has to wait is also an idea, like this:

Toast.makeText(getBaseContext(), "Spam protection, wait "+secondstowait,
        Toast.LENGTH_SHORT).show();

Can all this be done?

Solution

Why not record the timestamp the first time the button is clicked

, and then compare the time the button is clicked again to see if the difference is greater than the amount of time allotted?

Related Problems and Solutions