私は音楽プレーヤーをプログラミングしていて、音楽はバックグラウンドで再生されますService
。ユーザーがActivity
ホスト3を強制終了しFragments
、再度再起動すると、現在再生中の曲に関する情報と、ユーザーがセッションに追加した曲のリストを含むからActivity
を送信します。Broadcast
Service
問題は、最後の情報を設定したいときはいつでも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);
}
}