Skip to content

Instantly share code, notes, and snippets.

@undownding
Created June 18, 2016 22:02
Show Gist options
  • Save undownding/d164918e3b81bcbacbe10f8f2b5aa502 to your computer and use it in GitHub Desktop.
Save undownding/d164918e3b81bcbacbe10f8f2b5aa502 to your computer and use it in GitHub Desktop.
package me.undownding.music.service
import android.app.Service
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.ServiceConnection
import android.os.IBinder
import rx.Observable
import rx.Subscriber
/**
* Created by undownding on 16-6-19.
*/
class RxPlayingServiceImpl {
companion object {
var serviceConnection: RxConnection? = null
fun bind(context: Context): Observable<PlayingService> {
return Observable.create<PlayingService> {
if (serviceConnection == null) {
serviceConnection = RxConnection(it)
}
context.bindService(Intent(context, PlayingService::class.java), serviceConnection, Service.BIND_AUTO_CREATE)
}
}
}
class RxConnection(it: Subscriber<in PlayingService>): ServiceConnection {
val it = it
override fun onServiceConnected(name: ComponentName?, service: IBinder?) {
it.onNext((service as PlayingService.ServiceBinder).service)
}
override fun onServiceDisconnected(name: ComponentName?) {
it.onCompleted()
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment