-1

I want to make a program that will show an alert dialog with a confirmation button on it if it pressed it will continue and if not pressed for like 30 seconds will do something else. So far I get the gist of it for making the alert dialog show but later on if not pressed that I'm still working on it

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • you can use handler for this when you open the alert dialog run your time if use click on it just cancel the handler or if user not click on it than performe the action you want after handler finished – Amit pandey Jan 19 '21 at 12:38
  • i did consider using handler but so far i haven't got an example of it – Whiliams Thunardy Jan 19 '21 at 12:41
  • https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android – User Rebo Jan 19 '21 at 16:43
  • 2
    Does this answer your question? [How to call a method after a delay in Android](https://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android) – User Rebo Jan 19 '21 at 16:50
  • well the link you give me was used for another project of mine but this one kinda different – Whiliams Thunardy Jan 20 '21 at 06:16

1 Answers1

0
import android.os.Handler

class DelayHandler {
var handler: Handler? = null
var runable: Runnable? = null

init {
    handler = Handler()

}

fun startDealay(sec: Long, listner: () -> Unit) {

    runable = Runnable { listner.invoke() }
    handler?.postDelayed(runable!!, sec)
}

fun cancelHandler() {
    if (handler != null) {
        runable?.let { handler?.removeCallbacks(it) }
    }
}
}

after you just call this where you want a handeler

  private val delayHandler: DelayHandler? = DelayHandler()

     delayHandler?.cancelHandler() // call this for cancel the handler on button click
            
     delayHandler?.startDealay(30 * 1000) { // 30 *1000 for 30 sec
                // your action
            }
Amit pandey
  • 1,149
  • 1
  • 4
  • 15