この例のように、いくつかのタスクを実行する Android 開始サービスがあるとします。
・サービス開始
-タスク A を実行し、タスク B を実行してから、ファイル 1 をダウンロードし、ファイル 2 をダウンロードして、最後にタスク C を実行します。
-サービス停止
サービスがタスク B を実行しているときに、アクティビティがバインドされているとします。アクティビティ UI を更新するためにサービスが現在実行しているタスクを確認する正しい方法は何ですか?
私が今行っている方法は、処理中に各タスクのブール値フラグを作成し、アクティビティがサービスにバインドされたときに、どのフラグが true であるかをチェックして、正しいコールバックをアクティビティに送信することです。それは機能しますが、タスクが増えると、エラーが発生すると、より複雑になり、さらに困難になります。
private boolean doingTaskA = false;
private boolean doingTaskB = false;
.
.
public void doTaskA() {
// Task started, set the flag to true.
doingTaskA = true;
// send callback to update activity UI.
callback.onDoingTaskA();
// doing some API calls that takes some time to retrieve information
...
// Task finished, set to false.
doingTaskA = false;
}
public void doTaskB() {
// Task started, set the flag to true.
doingTaskB = true;
// send callback to update activity UI.
callback.onDoingTaskB();
// doing some API calls that takes some time to retrieve information
// while doing in background, an activity binds to this service (assuming
// not already bound).
...
// Task finished, set to false.
doingTaskB = false;
}
public IBinder onBind(Intent intent) {
// when binding check to see what the service is doing.
if(doingTaskA){
// send callback to update activity UI.
callback.onDoingTaskA();
}
if(doingTaskB){
// send callback to update activity UI.
callback.onDoingTaskB();
}
...
}
それを行うための効率的で信頼できる方法を教えてください。
ありがとう!