2017-05-28 26 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"; 
    

    再次给出正确答案的用户!

    相关问题