Delicious Software
Delicious Software

Reputation: 131

Service not handling Intent when called from Receiver

Rarely I have an issue where a phone will get in a state (known as "funny state") where my Intent Services won't get a startService command from a Broadcast Receiver. (yes, the manifest has the receivers and services defined).

In this example I am listening for push notifications then calling a CheckinService. Receiver:

public class PushReceiver extends BroadcastReceiver {

private static final String LOG_TAG = "push_receiver";

@Override
public void onReceive(Context context, Intent intent) {
    logger.putExtra("debug", "Received Push");

    Intent serviceIntent = new Intent(context, CheckinService.class);
    context.startService(serviceIntent);

    logger.putExtra("debug", "Sent to Service");
}

Service:

public class CheckinService extends IntentService {
    private static final String LOG_TAG = "checkin_service";
    public static final int SERVICE_ID = 3;

    public CheckinService() {
        super(LOG_TAG);
        Log.i(LOG_TAG, "Service says: Checkin service started no constructor");
    }

    public CheckinService(String name) {
            super(LOG_TAG);
        Log.i(LOG_TAG, "Service says: Checkin service started with constructor");
    }

    @Override
    protected void onHandleIntent(Intent intent) {
         Log.i(LOG_TAG, "Auto Checkin started");
         .....tons of genius logic.....
        }
}

So when the phone gets in the funny state the "received push" gets logged and the "sent to service" gets logged but the constructors and onHandleIntent methods of the service never get called.
I also have this happen not only on pushes but on receivers for inexactRepeatingAlarm and perhaps others but these two have been confirmed for sure.

Again this is very, very, rare and seems to happen after the phone has been left unused for a period of time; and perhaps goes into a power saving mode.

Also, terminating the application's process clears this up.

Upvotes: 1

Views: 229

Answers (1)

Delicious Software
Delicious Software

Reputation: 131

I realized what was happening here.

The IntentService is single threaded. So if something in my " .....tons of genius logic....." was blocking (like a http request with no timeout) the next intent that came into the service would not be processed.

Nice and humbling.

Upvotes: 1

Related Questions