1

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!

Upendra Joshi
  • 573
  • 5
  • 15
  • 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

4 Answers4

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,264
  • 20
  • 49
  • 1
    I 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

1) create a service and put timer code in it

2) in service , after your time finished broadcast an intent

3) in your activity write a broadcast receiver and in broadcast receiver Enable button again.

for running service after closing app look at :How to keep a service running in background even after user quits the app?

for send intent from service to activity look at : send intent from service to activity

Some Tested Codes

1)creating service :

public class TimeService extends IntentService {
    public TimeService() {
        super("TimeService");
    }

@Override
protected void onHandleIntent(Intent intent) {

   //Your Time Code

    //broadcast intent in your app 
    Intent RTReturn = new Intent(MainActivity.RECEIVE);
    RTReturn.putExtra("state", "done");
    LocalBroadcastManager.getInstance(this).sendBroadcast(RTReturn);
}

2)your activity

-create action string

//Your activity will respond to this action String
public static final String RECEIVE = "TIMING";

-create BroadcastManager

private LocalBroadcastManager bManager;

-create a broadcastReceiver

private BroadcastReceiver bReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if(intent.getAction().equals(RECEIVE)) {
            String serviceString = intent.getStringExtra("state");
            if(serviceString.equals("done"))
                 // enable your button 
                 btnEnable();
        }
    }
};

-put intent filter in onCreate()

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

        //make an intent filter
        bManager = LocalBroadcastManager.getInstance(this);
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(RECEIVE);
        bManager.registerReceiver(bReceiver, intentFilter);


     }

add it in your button on click

//start time service
Intent intent = new Intent(this,TimeService.class);
startService(intent);

at the end remember two things:

first -> add service to your manifest

second -> unregister broadcast receiver in onDestroy()

Community
  • 1
  • 1
K327
  • 133
  • 2
  • 9
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
  • 726
  • 3
  • 12
-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,529
  • 5
  • 25
  • 37