I'm new to android.I'm creating an app in which i have to disable the button after it is clicked, and enable it again after 5 minutes. Count down timer should continue even after app is closed. any help is appreciated. Thanks in advance!
Asked
Active
Viewed 4,602 times
1
-
Try using Handler – ρяσѕρєя K Dec 27 '16 at 07:07
-
@ρяσѕρєяK will Handler work if app is closed? – Mrinmoy Dec 27 '16 at 07:07
-
No, Handler will stop when app closed – ρяσѕρєя K Dec 27 '16 at 07:09
-
You can create a service which will notify the listening components – Rahul Dec 27 '16 at 07:10
-
I would suggest you to use services, and add the state in SharedPreference when the button is clicked and startService which should automatically change the value of SharedPreference and you button should be enabled again – Mrinmoy Dec 27 '16 at 07:13
-
Have you tried something to disabled the button for 5minutes ? Not talking about the seconds problem (persistance after shutdown). What happen after the 5minutes ? Same question when the app is closed – AxelH Dec 27 '16 at 07:16
-
The easiest way is using sharedPreferences.. comparing time click with current time when user load the activity or you can regularly run a Handler for every 10 seconds. (**the handle only run in current activity only**) – ZeroOne Dec 27 '16 at 07:24
3 Answers
7
You can store time in shared preference by following code.
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
SharedPreferences.Editor editor = settings.edit();
editor.putString("click_time", your_time);
Whenever need to compare, just compare your current time with the time stored in preference. Check if difference between time is less than 5 min or not.
TO get time from prefernce:
SharedPreferences settings = c.getSharedPreferences(PREFS_NAME, 1);
String storedTime=settings.getString("clic_time", "");
Get difference in minute:
Date current, Date previous;
long diff = current.getTime() - previous.getTime();
long minutes = diff / (60 * 1000);
To show timer, when app comes from background, you can get time from preference and start timer by using difference between 2 times.

Beena
- 2,334
- 24
- 50
-
1I was to lazy to write this, I would use the same solution. Just storing the time when to enabled the button again. Using a simple Timer and rebuilding it `onCreate` to end on the saved timed. No need to use Service on anything. – AxelH Dec 27 '16 at 07:22
-
after putting value in shared preference, you should commit it. .editor.commit(); – Dante Oct 08 '18 at 06:02
0
private long mLastClickTime = 0;
public static final long MAX_CLICK_INTERVAL = 1000; // here you can pass the time interval you want the user to wait for the next click
// and paste the below code on button click
if (SystemClock.elapsedRealtime() - mLastClickTime < MAX_CLICK_INTERVAL) {
return;
}
mLastClickTime = SystemClock.elapsedRealtime();

Pranav Darji
- 744
- 3
- 13
-1
Try Handler
Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
// this will disable your button
yourButton.setEnabled(false);
}
};
yourButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// wait for 5 minutes
handler.postDelayed(runnable, 5*60*1000);
}
});

Vishal Chhodwani
- 2,567
- 5
- 27
- 40
-
Handler will work on main thread so that is not good way to solved this. – AJTEST Sep 04 '19 at 10:30