2

I have this IntentService (HttpService) which fetches raw json data from a webservice:

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);

    }
}

Now from the IntentService that uses HttpService I'm trying to get the broadcast:

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 I'm getting this error:

RestaurantModel has leaked IntentReceiver com.example.RestaurantModel$1@42374590 that was originally registered here. Are you missing a call to unregisterReceiver()?

So I tried unregistering the Receiver but it seems to need a Context to unregister the receiver.

How to receive values from an IntentService into another IntentService?

Jeka
  • 1,600
  • 3
  • 22
  • 36

2 Answers2

1

The best answer is: have only one IntentService.

The next-best answer is: get rid of the broadcast stuff entirely, and have the first IntentService call startService() to kick off the second IntentService.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

Agree with @CommonsWare, in case you want to use BroadcaseReceiver inside of an IntentService, register it in the onCreate method and unregister it in the onDestroy method.

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);


}

}

Eric Liu
  • 1,516
  • 13
  • 19