我有一項服務,可以管理音樂播放。我也有活動發送用戶音樂意圖。當我打開活動時,我想獲得當前的播放狀態。從android服務獲取當前狀態
我有特定的播放器,只有兩個事件:播放開始和播放結束。所以如果我使用廣播,我只會得到下一個事件。 獲取它時,我將事件保存在變量lastAction
中。我可以創建新的命令ACTION_SEND_CURRENT_STATE
。但看起來不太好。
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
when (intent?.action) {
null -> {
player?.cancel()
}
ACTION_PLAY -> {
player?.cancel()
player = createPlayer(intent)
player?.start()
}
ACTION_STOP -> {
player?.cancel()
}
}
return START_STICKY
}
override fun onPlayingBegin(p0: player?) {
lastAction = BRODCAST_PLAYING_BEGIN
sendBroadcast(Intent(BRODCAST_PLAYING_BEGIN)
.putExtra(EXTRA_SONG, currentSong)
)
}
如何從服務中正確獲取當前狀態?作爲我指的是最後一項行動。
看到https://developer.android.com/guide/components/bound-services.html – pskink
@pskink我的服務工作在前臺 – punksta
它沒關係 – pskink