I am using Kotlin from quite some time now, but I could not able to achieve not-null types for all the properties in Kotlin.

After what I learned by watching Kotlin in Google IO and a bit of research, I made all properties not nullable.

Please check my code and correct me if any mistakes.

Thanks in advance.

class MusicService : Service(), PlaybackManager.PlaybackServiceCallback {

    private val mDelayedStopHandler = DelayedStopHandler(this)
    private val eventBus = EventBus.getDefault()

    lateinit var mSessionToken: MediaSessionCompat.Token
    lateinit var mTransportControls: MediaControllerCompat.TransportControls

    private val mSession: MediaSessionCompat by lazy { MediaSessionCompat(this, "MusicService") }
    private val mMediaNotificationManager: MediaNotificationManager by lazy {
        try {
        } catch (e: RemoteException) {
            throw IllegalStateException("Could not create a MediaNotificationManager", e)
    val mPlaybackManager: PlaybackManager by lazy {
        PlaybackManager(mPlayback = LocalPlayer(this), mServiceCallback = this)

    override fun onCreate() {
        LogHelper.d(TAG, "onCreate")

        //Init MediaSessionCompat and TransportControls
        mSessionToken = mSession.sessionToken
        mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS or MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS)
        mTransportControls = MediaControllerCompat(this, mSession).transportControls

        //EventBus Reg


    fun onGetAllMediaEventResponse(event: GetAllMediaEventResponse) {
        Timber.d("GetAllMediaEventResponse event.status = ", event.status)
        mPlaybackManager.mMediaData = event.mediaItems
  • \$\begingroup\$ looks like everything is correctly converted! \$\endgroup\$ – dimsuz Jun 14 '17 at 22:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.