在這裏我正在運行音樂播放服務。 此代碼段是我在onStart()我的活動即使在活動被破壞後,如何使服務保持活躍狀態?
if(musicServiceStartIntent == null) {
musicServiceStartIntent = new Intent(this, MusicService.class);
startService(musicServiceStartIntent);
bindService(musicServiceStartIntent, musicConnection, Context.BIND_AUTO_CREATE);
}
第一的方法,我開始我的服務,然後綁定它。我在onDestroy()方法中調用unbindservice()。我的活動遭到破壞,服務停止。
unbindService(musicConnection);
清單文件聲明
<service android:name=".Services.MusicService"/>
我怎能我的服務在後臺運行,甚至破壞活動後。我提到幾個StackOverflow的線程,但它們沒有幫助。
http://stackoverflow.com/questions/22895872/how-to-keep-a-service-with-listeners-alive-after -unbind-in-android試試這個。這對我工作 –
不解除Ondestroy()上的服務,然後也銷燬,然後使服務粘滯。 – Aditi
我試過你的答案。如果我不解除綁定我的服務onDestroy(),它會給出錯誤。 – saa