Created
April 9, 2019 05:37
-
-
Save granoeste/30b741bc8325ce86ec6c474a3991bddb to your computer and use it in GitHub Desktop.
時間以内に2回タップしたら起きるプログラム on Activity with CoroutineScope
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class CoroutineScopeActivity : AppCompatActivity(), CoroutineScope { | |
lateinit var job: Job | |
override val coroutineContext: CoroutineContext | |
get() = Dispatchers.Main + job | |
private var goBackToSleepJob: Job? = null | |
private var count: Int = 0 | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
job = Job() | |
// twice tap | |
binding.button.setOnClickListener { | |
count++ | |
if (count >= 2) { | |
Timber.d("Wake Up!") | |
} | |
goBackToSleepJob?.cancel() | |
goBackToSleepJob = launch { | |
delay(5_000) | |
Timber.d("Go Back To Sleep!") | |
count = 0 | |
} | |
goBackToSleepJob?.start() | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment