Android IntentService 以空意图触发

Android IntentService triggered with null intent

我在 Crashlytics 中发现崩溃:

Fatal Exception: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Intent.getIntExtra(java.lang.String, int)' on a null object reference at com.myapp.APKOfferQueueManagerIntentService.onHandleIntent(SourceFile:71) at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.os.HandlerThread.run(HandlerThread.java:61)

IntentService 怎么会被空意图触发?

IntentService 可能会像所有其他服务一样停止,但它将由 "the system" 重新启动,直到 onHandleIntent() 完成其工作。在这种情况下,documentation 表示 intent 参数

... may be null if the service is being restarted after its process has gone away

要始终获取原始 Intent 作为参数,请使用

setIntentRedelivery(true);

IntentService.

的构造函数中

另请注意

If multiple Intents have been sent, only the most recent one is guaranteed to be redelivered.