zihadrizkyef

Untitled

Dec 3rd, 2017
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 0.89 KB | None | 0 0
  1. override fun onCreate(savedInstanceState: Bundle?) {
  2.         super.onCreate(savedInstanceState)
  3.         setContentView(R.layout.activity_main)
  4.  
  5.         val observable = Observable
  6.                 .just(longRunningOperation())
  7.  
  8.         btStartOperation.setOnClickListener {
  9.             observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(
  10.                     { text -> Toast.makeText(this, text, Toast.LENGTH_SHORT).show() },
  11.                     { error -> error.printStackTrace() },
  12.                     { /*Toast.makeText(this, "complete", Toast.LENGTH_SHORT).show()*/ }
  13.             )
  14.         }
  15.     }
  16.  
  17.     private fun longRunningOperation(): String {
  18.         return try {
  19.             Thread.sleep(5000)
  20.             "Mantap bos!"
  21.         } catch (e: InterruptedException) {
  22.             e.printStackTrace()
  23.             "ga mantap ah"
  24.         }
  25.     }
Advertisement
Add Comment
Please, Sign In to add comment