简体   繁体   中英

How to pause Service and wait till other service is finished

I created a Service TimingService Service

public class TimingService extends Service {

that includes 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 triggers intentDirty and this intent reports status with ResponseReciver.

Now I would like to pause void handleMessage(Message msg) and its for(int i = 0; i<3; i++) till the intentDirty is finished. How to do it via BroadcastReciver and onRecive callback?

Create the Broadcast Receiver dynamically in TimingService and register it for the appropriate event through Local Broadcast Manager. In your other service, raise the event through Local Broadcast Manager.

An alternative is to use some kind of locking strategy (for example, through a Mutex).

I realize you're writing in Java but in C# one way you could do this is through a ManualResetEvent, which is a class that allows one thread to wait until another thread "signals" it. You could try something similar in Java. (Sorry about not being more specific on the final point, I generally write my Android apps in C#/Xamarin).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM