文章会不停修改和完善。
1、kotlin 单例
class App : Application(){ companion object { private var instance: Application? = null //var btDev: BtDevice? = null fun instance() = instance!! } override fun onCreate() { super.onCreate() instance = this //btDev = BtDevice.getInstance(instance!!) } }
var instance: MusicPlayer? = null fun instance(): MusicPlayer { if (instance == null) instance = MusicPlayer() return instance!! } fun instance(): MusicPlayer { if (instance == null) { synchronized(MusicPlayer::class) { if (instance == null) { instance = MusicPlayer() } } } return instance!! }