【问题标题】:Android Service onStartCommand() fired when the app force closes应用强制关闭时触发 Android 服务 onStartCommand()
【发布时间】:2013-07-16 13:02:05
【问题描述】:

我从Activity 中启动Service,然后将活动绑定到它。 如果我尝试从任务管理器强制关闭应用程序,则会调用 Service onStartCommand() 方法并调用 NullPointerException。我不明白为什么要调用 onStartCommand()。

Service 目前刚刚覆盖了 onStartCommand():

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle bundle = intent.getExtras();  //This is line 24
    //some stuff to get strings from bundle
    return super.onStartCommand(intent, flags, startId);
}

例外:

07-16 14:49:02.190: E/AndroidRuntime(24607): FATAL EXCEPTION: main
07-16 14:49:02.190: E/AndroidRuntime(24607): java.lang.RuntimeException: Unable to start service com.app.GameService@420edf78 with null: java.lang.NullPointerException
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2782)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.app.ActivityThread.access$2000(ActivityThread.java:152)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1385)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.os.Looper.loop(Looper.java:137)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.app.ActivityThread.main(ActivityThread.java:5328)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at dalvik.system.NativeStart.main(Native Method)
07-16 14:49:02.190: E/AndroidRuntime(24607): Caused by: java.lang.NullPointerException
07-16 14:49:02.190: E/AndroidRuntime(24607):    at com.app.GameService.onStartCommand(GameService.java:24)
07-16 14:49:02.190: E/AndroidRuntime(24607):    at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:2765)
07-16 14:49:02.190: E/AndroidRuntime(24607):    ... 10 more

【问题讨论】:

  • 您的活动/服务的 onDestroy() 中有代码吗?

标签: android service android-activity nullpointerexception


【解决方案1】:

你可以这样使用某事

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    Bundle bundle = getIntent.getExtras();
    if (bundle!=null) {
        Bundle bundle = intent.getExtras(); 
        }
    } else {
        Log.i("Log", "Bundle is null");

    }
}

【讨论】:

  • getIntent() 没有为服务定义。我解决了检查intent != null但我不明白为什么 onStartCommand() 被调用。
【解决方案2】:

可能从 onStartCommand() 返回 Service.Start_not_sticky 是您想要的。我不知道你的情况,试一试。

这将有助于更好地了解完成/重新启动服务的不同模式 - http://developer.android.com/reference/android/app/Service.html#START_NOT_STICKY

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多