2

我有這個IntentService(HttpService)從web服務獲取原始的JSON數據:如何從另一個IntentService中的IntentService接收值?

public class HttpService extends IntentService { 

    public static final String BROADCAST_ACTION = "com.example.HttpService"; 

    public HttpService() { 
     super("HttpService"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 

     //code to get a String with jsonData 


     //Then I send a broadcast 
     Intent broadcastHttpResponseIntent = new Intent(); 
     broadcastHttpResponseIntent.setAction(BROADCAST_ACTION); 
     broadcastHttpResponseIntent.putExtra("jsonData", jsonData); 

     sendBroadcast(broadcastHttpResponseIntent); 

    } 
} 

從使用HttpService我試圖讓廣播IntentService現在:

public class RestaurantModel extends IntentService { 

    public static final String BROADCAST_ACTION = "com.example.RestaurantModel"; 

    public RestaurantModel() { 
     super("RestaurantModel"); 
    } 

    @Override 
    protected void onHandleIntent(Intent intent) { 
     Log.v("RestaurantModel", "onHandleIntent"); 

     BroadcastReceiver httpBroadcastReceiver = new BroadcastReceiver() { 
      @Override 
      public void onReceive(Context context, Intent intent) { 
       Log.v("RestaurantModel", "onReceive"); 

       String jsonResponse = intent.getStringExtra("jsonData"); 

      } 
     }; 

     Intent getRestaurantsJsonIntent = new Intent(RestaurantModel.this, HttpService.class); 
     getRestaurantsJsonIntent.putExtra("urlRestaurants", intent.getStringExtra("urlRestaurants")); 
     startService(getRestaurantsJsonIntent); 

     registerReceiver(httpBroadcastReceiver, new IntentFilter(HttpService.BROADCAST_ACTION)); 
    } 
} 

SO我得到這個錯誤:

RestaurantModel has leaked IntentReceiver [email protected] that was originally registered here. Are you missing a call to unregisterReceiver()?

所以我試圖取消註冊接收器,但它似乎需要一個上下文來註銷 收件人。

如何將IntentService的值接收到另一個IntentService中?

回答

1

最好的答案是:只有一個IntentService

次最好的答案是:完全擺脫廣播內容,並讓第一個IntentService致電startService()啓動第二個IntentService

1

同意@CommonsWare,如果您想在IntentService中使用BroadcaseReceiver,請在onCreate方法中註冊它,並在onDestroy方法中取消註冊它。

public class RestaurantModel extends IntentService { 

public static final String BROADCAST_ACTION = "com.example.RestaurantModel"; 

private BroadcastReceiver httpBroadcastReceiver; 

public RestaurantModel() { 
    super("RestaurantModel"); 
} 

@Override 
public void onCreate() { 
    super.onCreate(); 
    httpBroadcastReceiver = new BroadcastReceiver() { 
     @Override 
     public void onReceive(Context context, Intent intent) { 
      Log.v("RestaurantModel", "onReceive"); 

      String jsonResponse = intent.getStringExtra("jsonData"); 

     } 
    }; 

    LocalBroadcastManager.getInstance(getApplicationContext()).registerReceiver(httpBroadcastReceiver); 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    LocalBroadcastManager.getInstance(getApplicationContext()).unregisterReceiver(httpBroadcastReceiver); 
} 

@Override 
protected void onHandleIntent(Intent intent) { 
    Log.v("RestaurantModel", "onHandleIntent"); 


    Intent getRestaurantsJsonIntent = new Intent(RestaurantModel.this, HttpService.class); 
    getRestaurantsJsonIntent.putExtra("urlRestaurants", intent.getStringExtra("urlRestaurants")); 
    startService(getRestaurantsJsonIntent); 


} 

}