を終了するまで待つにはどうすればサービスTimingServiceに は、サービスを一時停止し、他のサービスが
// 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に状態を報告ServiceHandler
が含まpublic class TimingService extends Service {
Serviceを作成しました。
ここで、void handleMessage(Message msg)
とそのfor(int i = 0; i<3; i++)
を、intentDirtyが終了するまで一時停止したいと思います。 BroadcastReciverとonRecive
コールバックを使用して行う方法