2017-05-28 28 views
0

我會直接指出這一點。爲什麼在調用兩個Intent服務時一個接一個地出現無限循環?

我需要消費一些Google網絡服務(DistanceMatrix and Geocoding APIs),然後才能繼續下一步就必須獲得這兩個響應。我剛剛遇到了IntentServices,我讀到這可能是執行一項任務,然後是另一項任務的好方法,因爲它們可以使用IntentsBroadcastReceivers進行通信。這樣,第二個IntentService將知道第一個IntentService何時完成它的工作。

以下步驟總結了我想實現的目標。從DistanceMatrix API

  • 一旦這個任務完成後,啓動其他意圖的服務和 傳入響應,這樣我可以從 第二意圖服務中獲取IP地址,並使用地理編碼這一個

    1. 獲取響應Geocoding API

    然而,在我做這個實現之前,測試代碼發送一些消息到控制檯只是爲了確保兩個意圖服務可以相互溝通,但我得到一個無限循環。

    I/MapFragment: DistanceMatrixService finished... 
    I/MapFragment: Running GeocodingService... 
    I/MapFragment: GeocodingService finished... 
    I/MapFragment: The whole process has finished... 
    I/MapFragment: DistanceMatrixService finished... 
    I/MapFragment: Running GeocodingService... 
    I/MapFragment: GeocodingService finished... 
    I/MapFragment: The whole process has finished... 
    I/MapFragment: DistanceMatrixService finished... 
    I/MapFragment: Running GeocodingService... 
    I/MapFragment: GeocodingService finished... 
    I/MapFragment: The whole process has finished... 
    I/MapFragment: DistanceMatrixService finished... 
    I/MapFragment: Running GeocodingService... 
    I/MapFragment: GeocodingService finished... 
    I/MapFragment: The whole process has finished... 
    

    第一IntentService點擊按鈕

    @Override 
    public void onClick(View v) { 
        Intent msgIntent = new Intent(getActivity(), DistanceMatrixService.class);   
        getActivity().startService(msgIntent); 
    } 
    

    的DistanceMatrixService類只是將消息發送回給主片段時獲取調用。

    public class DistanceMatrixService extends IntentService { 
    
        public static final String ACTION_FINISHED = "ACTION_FINISHED"; 
    
        public DistanceMatrixService() { 
         super("DistanceMatrixService"); 
        } 
    
        @Override 
        protected void onHandleIntent(Intent intent) { 
    
         // Run the heavy task just here 
         //... 
    
         // Once the job is done send a broadcast message to communicate that it has finished 
         Intent bcIntent = new Intent(); 
         bcIntent.setAction(ACTION_FINISHED); 
         sendBroadcast(bcIntent); 
        } 
    } 
    

    當第一IntentService已經完成了GeocodingServices得到啓動

    private class DistanceMatrixBroadcastReceiver extends BroadcastReceiver{ 
    
        @Override 
        public void onReceive(Context context, Intent intent) { 
          if(intent.getAction().equals(DistanceMatrixService.ACTION_FINISHED)){ 
           Log.i(LOG_TAG, "DistanceMatrixService finished..."); 
           Log.i(LOG_TAG, "Running GeocodingService...");      
    
           Intent msgIntent = new Intent(getActivity(), GeocodingService.class); 
           getActivity().startService(msgIntent); 
          } 
        } 
    
    } 
    

    像第一IntentService這一個只是將消息發送到主片段

    public class GeocodingService extends IntentService{ 
    
        public static final String ACTION_FINISHED = "ACTION_FINISHED"; 
    
        public GeocodingService() { 
         super("GeocodingService"); 
        } 
    
        @Override 
        protected void onHandleIntent(Intent intent) { 
    
         // Run the heavy task just here 
         //... 
    
         // Once the job is done send a broadcast message to communicate that it has finished 
         Intent bcIntent = new Intent(); 
         bcIntent.setAction(ACTION_FINISHED); 
         sendBroadcast(bcIntent); 
        } 
    
    } 
    

    而且GeocodingBroadcastReceiver類漁獲消息

    private class GeocodingBroadcastReceiver extends BroadcastReceiver{ 
    
        @Override 
        public void onReceive(Context context, Intent intent) { 
         if(intent.getAction().equals(GeocodingService.ACTION_FINISHED)){ 
          Log.i(LOG_TAG, "GeocodingService finished..."); 
          Log.i(LOG_TAG, "Running The process has finished..."); 
         } 
        } 
    
    } 
    

    最後,這是代碼以註冊即所謂的onCreate()方法

    private void registerGeocodingReceiver(){ 
        IntentFilter filter = new IntentFilter(); 
        filter.addAction(GeocodingService.ACTION_FINISHED); 
        GeocodingBroadcastReceiver receiver = new GeocodingBroadcastReceiver(); 
        getActivity().registerReceiver(receiver, filter); 
    } 
    
    private void startDistanceMatrixReceiver(){ 
        Intent msgIntent = new Intent(getActivity(), DistanceMatrixService.class); 
        msgIntent.putExtra("iteraciones", 10); 
        getActivity().startService(msgIntent); 
    } 
    
  • +1

    您對廣播和兩個接收器都使用相同的操作 - 「ACTION_FINISHED」。 –

    +2

    我認爲你需要讓你的'ACTION_FINISHED'字符串是唯一的。本質上,您同時使用相同的過濾器註冊了'BroadcastReceiver'。 – Dave

    +0

    是的,這是問題,我只是改變了兩個類ACTION_FINISHED的值。謝謝你們。 – Sandoval0992

    回答

    0

    的問題是,這兩種服務具有恆定的聲明具有相同值

    GeocodingService.ACTION_FINISHED = "ACTION_FINISHED"; 
    DistanceMatrixService.ACTION_FINISHED = "ACTION_FINISHED"; 
    

    剛分配兩個BroadcastReceivers對每個人都有不同的價值。

    GeocodingService.ACTION_FINISHED = "GEOCODING_FINISHED"; 
    DistanceMatrixService.ACTION_FINISHED = "DISTANCE_MATRIX_FINISHED"; 
    

    再次給出正確答案的用戶!

    相關問題