0

I want to put something like a timer on the button, that is, I have a button, after pressing it becomes inactive, say for 2 hours, after two hours it is clickable again, but so that the timer keeps counting time even if the application is closed or minimized. How to make the button inactive, I know, help to understand how to make a timer.

I wrote the code so that when I pressed the button, she would write down the date and deactivate the button, and after that she would get the new date and find the difference, and when it is 10 seconds, she activates the button again, but the code does not work, explain why?

public class MainActivity extends AppCompatActivity {

Button button;
TextView textView;
private String TAG;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    button = findViewById(R.id.button);
    textView = findViewById(R.id.textView);
    final SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);




    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            button.setEnabled(false);
            SharedPreferences.Editor editor = preferences.edit();
            long startTime = System.currentTimeMillis();
            editor.putLong("Time", startTime);
            editor.apply();
            Log.d(TAG, "Time " + startTime);
        }
    });

    long startMillis = preferences.getLong("Time", 0);
    long now = System.currentTimeMillis();
    Log.d(TAG, "Now " + now);
    long difference = now - startMillis;

    if (difference == 10000) {
        button.setEnabled(true);
    }
    Log.i(TAG, "Time111: " + difference);

}

@Override
protected void onStart() {
    super.onStart();

}

}

  • please check this [question](https://stackoverflow.com/questions/31639105/start-timer-on-button-click) may help – Dr Mido Mar 01 '19 at 01:03

2 Answers2

0

I would just save the last date time it was clicked (e.g. in a SharedPreference) and every time the activity/fragment becomes active you check the value and subtract it to the current date time.

If your app is a public one where the user has the ability to change the device clock you can configure a BroadcastReceiver that triggers every time the clock changes and block you app or take the corresponding action.

If this button triggers and action in some API of your own, the best way to handle it would be to record the last time in your server. Remember that we are limited in so many ways in the client app.

ingkevin
  • 437
  • 5
  • 19
  • I sort of did as you said, but does not work, tell me what's wrong, please (question edited) – Ara Gevorgyan Mar 02 '19 at 20:33
  • @AraGevorgyan the problem is your condition `difference == 10000` it will almost never be met. Try something like `difference >= 10000`. – ingkevin Mar 10 '19 at 13:52
  • Yes, I also noticed a stupid mistake) But now I don’t understand how to make sure that after pressing the button she checks the time difference every second – Ara Gevorgyan Mar 11 '19 at 19:38
  • There are many ways to solve a problem, I cannot tell wich one will fit your requirements the best. A couple of ideas would be to start a thread that loops every 2 secs, another one will be to configure an Alarm that wakes up your app and changes the buttons state. – ingkevin Mar 12 '19 at 13:49
0

You can check the current time from API and save it in Preferences when the button is clicked and enable/disable the button according to that.

Mayank jain
  • 497
  • 6
  • 9