2016-08-04 1 views
0

を終了するまで待つにはどうすればサービス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コールバックを使用して行う方法

答えて

0

TimingServiceでブロードキャスト受信者を動的に作成し、Local Broadcast Managerを使用して適切なイベント用に登録します。他のサービスでは、ローカルブロードキャストマネージャーを使用してイベントを発生させます。

代わりに、何らかの種類のロック戦略(たとえば、Mutexを使用)を使用することもできます。

私はあなたがJavaで書いていることを認識していますが、これを行う方法の1つはManualResetEventです。これは別のスレッドがシグナルを送るまで待つクラスです。あなたはJavaで同様のことを試すことができます。 (最終的には具体的ではないことを申し訳ありませんが、私は通常、C#/ XamarinでAndroidアプリを作成しています)。

関連する問題