我正在编写音乐播放器,音乐在后台播放Service。当用户杀死Activitywhich hosts 3 Fragments,然后再次重新启动时,我从Activity发送一个包含有关当前播放歌曲的信息以及用户添加到其会话的歌曲列表的信息。BroadcastService
问题是,每次我想将最后的信息设置为什么都Fragments没有发生,因为它们的创建时间太长,并且Broadcast没有得到应有的处理。
我怎样才能让Service或Broadcast等到创建片段以便适当处理它们?
以下是相关的代码片段:
//When the activity binds to the service, refresh the fragments
private ServiceConnection conn = new ServiceConnection() {
public void onServiceConnected(ComponentName className, IBinder service) {
LocalBinder binder = (LocalBinder) service;
myService = binder.getService();
myService.setBound(true);
if(myService.startedOnce) {
myService.refreshFragments();
}
myService.startedOnce = true;
}
}
//This is the broadcast receiver of the Fragment
//it receives the broadcast too soon, and I can't set the
//Views so they are always empty.
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (action.equals(MusicService.REFRESH_ALL)) {
Song current = intent.getParcelableExtra("song");
setCurrentSong(current);
}
}