Skip to content

Instantly share code, notes, and snippets.

View aanandshekharroy's full-sized avatar
👋

Aanand Shekhar Roy aanandshekharroy

👋
View GitHub Profile
class RandomNumberGeneratorService:Service() {
val binder: IBinder = RandomNumberGeneratorServiceBinder()
inner class RandomNumberGeneratorServiceBinder : Binder(){
val service: RandomNumberGeneratorService = this@RandomNumberGeneratorService
}
override fun onBind(intent: Intent): IBinder {
return binder
}
}
var randomNumberGeneratorService: RandomNumberGeneratorService? = null
private lateinit var connection: ServiceConnection
var bounded = false
val TAG = MainActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
connection = object : ServiceConnection{
override fun onServiceDisconnected(componentName: ComponentName) {
Log.d(TAG,"Service disconnected.")
class MainActivity : AppCompatActivity() {
var randomNumberGeneratorService: RandomNumberGeneratorService? = null
private lateinit var connection: ServiceConnection
var bounded = false
val TAG = MainActivity::class.java.simpleName
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
connection = object : ServiceConnection{
override fun onServiceDisconnected(componentName: ComponentName) {
class RandomNumberGeneratorService:Service() {
private val binder: IBinder = RandomNumberGeneratorServiceBinder()
private lateinit var handlerThread:HandlerThread
private lateinit var handler: Handler
var randomNumber: Int = -1
private val TAG = RandomNumberGeneratorService::class.java.simpleName
private lateinit var mNM: NotificationManager
private val NOTIFICATION = R.string.local_service_started
private val HANDLER_THREAD_NAME="random_number_generator_thread"
private lateinit var notificationBuilder: NotificationCompat.Builder
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.retry(3)
.subscribeBy(
onNext = {
println(it)
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.retry()
.subscribeBy(
onNext = {
println(it)
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.onErrorReturn{
t: Throwable ->
if(t.message=="<something you want>"){
1 // Return a value based on error type
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.onErrorReturnItem (-1)
.subscribeBy(
onNext = {
println(it)
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.doOnError {
println("Doing on error")
}
.subscribeBy(
Observable.fromArray(1,2,3)
.doOnNext {
if(it==2){
throw (RuntimeException("Exception on 2"))
}
}
.onExceptionResumeNext(Observable.just(10))
.subscribeBy(
onNext = {
println(it)