Android IntentService 以空 Intent 触发

标签 android android-intent android-service android-intentservice

我在 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 怎么会被 null intent 触发?

最佳答案

IntentService 可以像所有其他服务一样停止,但它将由“系统”重新启动,直到 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.

关于Android IntentService 以空 Intent 触发,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37960917/

相关文章:

android - 如何以编程方式将布局添加到当前布局的末尾?

android - 无法同步对 notify() 的调用

java - 获取已安装应用程序所需的权限: Application Force Closes

android - 从内容 URI 获取搜索图像的绝对文件路径

android - 如何正确地扩大内容的大小并添加到 ViewPager?

android - 如何使用PackageManager.setComponentEnabledSetting

android - 如何使用 ShareCompat.IntentBuilder 启动 Activity

Android 服务不会在 Lollipop 中重新启动

android - 通过 IPC 将对象列表从服务传递到 Activity 时,Parcelable 项目消失

android - 服务中的 BroadcastReceiver 不接收广播 Android