1

I have the following IntentService.

public class NotificationIntentService extends IntentService {
    public NotificationIntentService() {
        super("NotificationIntentService");
    }

    @Override
    protected void onHandleIntent(@Nullable Intent intent) {
        boolean retainStockPriceAlert = intent.getBooleanExtra(RETAIN_STOCK_PRICE_ALERT, true);
        ...
    }
}

I start the IntentService using the following code

Intent intent = new Intent(this, NotificationIntentService.class);
intent.putExtra(NotificationIntentService.RETAIN_STOCK_PRICE_ALERT, retainStockPriceAlert);
startService(intent);

However, to my surprise, some users are getting null Intent during onHandleIntent. Hence, NPE happens.

From documentation https://developer.android.com/reference/android/app/IntentService.html#onHandleIntent(android.content.Intent)

Intent: The value passed to startService(Intent). This may be null if the service is being restarted after its process has gone away; see onStartCommand(Intent, int, int) for details.

I'm not quite understand what does it mean by restarted after its process has gone away.

I can easily avoid NPE by

@Override
protected void onHandleIntent(@Nullable Intent intent) {
    if (intent == null) {
        return;
    }

    boolean retainStockPriceAlert = intent.getBooleanExtra(RETAIN_STOCK_PRICE_ALERT, true);
    ...
}

But, I'm not sure whether that is a correct thing to do.

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

1

I able to avoid NPE by using

setIntentRedelivery(true);

in IntentService constructor

This answer is provided by @Kunu 's reference linkt - https://stackoverflow.com/a/37973523/72437

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875