在这里我正在运行音乐播放服务。 此代码段是我在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