Home > Mobile >  Using coroutines, pop-up dialog box to determine whether to continue or stop after waiting for the u
Using coroutines, pop-up dialog box to determine whether to continue or stop after waiting for the u

Time:03-12

Beginners Kotlin coroutines, powerful and complex, if wait for the user to select certain coroutines is suspended after, to write their own code is as follows, tests is sure
O and what's the problem with
Public class Fragment4Kt: fragments (), CoroutineScope by MainScope () {

//... Omit OnCreateView, onDestroy
//getHttpContent: pages GET request

Private var myJob: Job?=null

Private suspend fun myDelay (MSG: String)=coroutineScope {
Val job=async {
Try {
Delay (Long. MAX_VALUE)
} the finally {
Println (" waiting ")
}
}

Launch (Dispatchers. The Main) {
AlertDialog. Builder (activity)
SetMessage (MSG)
SetTitle (" title ")
SetCancelable (false)
. SetPositiveButton (" sure ") {_, _ - & gt; Job. The cancel ()}
{setNegativeButton (" cancel ") _, _ - & gt;
Job. The cancel ()
MyJob? The cancel ()
}
The create ()
The show ()
}
}

Fun test ()=launch (Dispatchers. IO) {
HttpUtils. GetHttpContent (" http://www.baidu.com ")
Println (" job1 is complete ")

MyDelay (" job2 continue?" )
HttpUtils. GetHttpContent (" http://www.163.com ")
Println (" job2 complete ")

MyDelay (" job3 continue ")
HttpUtils. GetHttpContent (" https://www.12306.cn ")
Println (" job3 finished ")
}
}
}
  • Related