0
我創建了一個服務TimingService Service如何暫停服務,等到其他服務完成
public class TimingService extends Service {
包含的ServiceHandler
// Handler that receives messages from the thread
private final class ServiceHandler extends Handler {
public ServiceHandler(Looper looper) {
super(looper);
}
@Override
public void handleMessage(Message msg) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
try {
for(int i = 0; i<3; i++){
Intent intentDirty = new Intent(TimingService.this, DirtyJobService.class);
startService(intentDirty);
// Instantiates a new DownloadStateReceiver
ResponseReceiver mDownloadStateReceiver = new ResponseReceiver();
IntentFilter mStatusIntentFilter = new IntentFilter(DirtyJobService.Constants.BROADCAST_ACTION);
mStatusIntentFilter.addCategory(Intent.CATEGORY_DEFAULT);
// Registers the DownloadStateReceiver and its intent filters
LocalBroadcastManager.getInstance(TimingService.this).registerReceiver(mDownloadStateReceiver, mStatusIntentFilter);
}
} catch (InterruptedException e) {
// Restore interrupt status.
Thread.currentThread().interrupt();
}
// Stop the service using the startId, so that we don't stop
// the service in the middle of handling another job
stopSelf(msg.arg1);
}
}
// Broadcast receiver for receiving status updates from the IntentService
private class ResponseReceiver extends BroadcastReceiver {
// Prevents instantiation
// private DownloadStateReceiver() {
// }
// Called when the BroadcastReceiver gets an Intent it's registered to receive
@Override
public void onReceive(Context context, Intent intent) {
}
}
TimingService觸發intentDirty這個意向報告與ResponseReciver狀態。
現在我想暫停void handleMessage(Message msg)
及其for(int i = 0; i<3; i++)
,直到intentDirty完成。如何通過BroadcastReciver和onRecive
回調?