无法销毁Activity(服务未注册)

11

*通过以下操作解决:* 我通过更改Activity的onDestroy()ServiceConnection::onServiceDisconnected()方法来解决了这个问题。

我添加了一个布尔变量boundToService,用于检查是否已经连接到名为Service的服务。 ServiceConnection如下:

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        myService = ((EventService.MyBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub
        Log.i(getPackageName(), "ServiceConnection::onServiceDisconnected() called");
        boundToService = false;
    }
};

还有onDestroy()方法:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
    Log.e(getPackageName(), "Destroying Activity");
    if (boundToService && mConnection != null) {
        doUnbindService();
    }
}

以下是在Activity中进行绑定和解绑的方法:

public void doBindService() {
    bindService(new Intent(this, EventService.class), mConnection,
            Context.BIND_AUTO_CREATE);
    boundToService = true;
}

public void doUnbindService() {
    unbindService(mConnection);
    boundToService = false;
}

如Guillaume所说,首先调用super.onDestroy()是必须的。

*解决结束*

我有一个前台服务Foreground Service来轮询GPS数据,而MapActivity每3秒请求一次该数据。当MapActivityonDestroy()被调用时,我会收到以下LogCat错误:

06-11 21:26:35.591: D/CLIPBOARD(14801): Hide Clipboard dialog at Starting input: finished by someone else... !
06-11 21:26:44.451: D/dalvikvm(14801): Debugger has detached; object registry had 1371 entries
06-11 21:26:44.451: D/AndroidRuntime(14801): Shutting down VM
06-11 21:26:44.456: W/dalvikvm(14801): threadid=1: thread exiting with uncaught exception (group=0x40c4b1f8)
06-11 21:26:44.476: E/AndroidRuntime(14801): FATAL EXCEPTION: main
06-11 21:26:44.476: E/AndroidRuntime(14801): java.lang.RuntimeException: Unable to destroy activity {com.project4.mtl/com.project4.mtl.EventActivity}: java.lang.IllegalArgumentException: Service not registered: com.project4.mtl.EventActivity$1@416179d8
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3124)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3142)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread.access$1200(ActivityThread.java:127)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1192)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.os.Looper.loop(Looper.java:137)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at java.lang.reflect.Method.invokeNative(Native Method)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at java.lang.reflect.Method.invoke(Method.java:511)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at dalvik.system.NativeStart.main(Native Method)
06-11 21:26:44.476: E/AndroidRuntime(14801): Caused by: java.lang.IllegalArgumentException: Service not registered: com.project4.mtl.EventActivity$1@416179d8
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.LoadedApk.forgetServiceDispatcher(LoadedApk.java:888)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ContextImpl.unbindService(ContextImpl.java:1211)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.content.ContextWrapper.unbindService(ContextWrapper.java:375)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at com.project4.mtl.EventActivity.onDestroy(EventActivity.java:175)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.Activity.performDestroy(Activity.java:4629)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1082)
06-11 21:26:44.476: E/AndroidRuntime(14801):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3111)
06-11 21:26:44.476: E/AndroidRuntime(14801):    ... 11 more`

当然,我的程序在大约175行周围引发了异常的代码:

@Override
protected void onDestroy() {
    // TODO Auto-generated method stub
    unbindService(mConnection);
    super.onDestroy();
}
如果你需要更多的代码片段,请告诉我。
*编辑*
这是ServiceConnection的代码:
public void doBindService() {
    //ComponentName service = startService(new Intent(this,
        //  EventService.class));
    bindService(new Intent(this, EventService.class), mConnection,
            Context.BIND_AUTO_CREATE);
}

mConnection 是 ServiceConnection 类型:

private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        myService = ((EventService.MyBinder) service).getService();
    }

    public void onServiceDisconnected(ComponentName name) {
        // TODO Auto-generated method stub

    }
};

服务在活动销毁之前被销毁,这种情况是否可能发生? - SirPentor
服务首先被销毁。 - tolgap
那就是核心问题了。它是如何被破坏的? - SirPentor
@Override public void onDestroy() { // TODO Auto-generated method stub Log.e(getPackageName(), "销毁服务"); super.onDestroy(); locationManager.removeUpdates(this); if(notificationManager!=null){ notificationManager.cancelAll(); } stopForeground(true); } - tolgap
1
我认为在解除绑定服务之前,在函数中首先要执行super.onDestroy(); - Guillaume
显示剩余3条评论
2个回答

4
这对我很有帮助!只需从getApplicationContext() 而不是 "this" 上下文调用函数unbindService(),就可以为我工作了。 (绑定服务同理)
getApplicationContext().unbindService(mServiceConnection);

当您从getApplicationContext()调用它时,它真的是无约束的吗? - Dmitry
我在应用程序上下文方面遇到了相同的问题。 - Dmitry

3

您可以在onServiceDisconnected中将mService设置为null,并在onDestroy中检查。像这样:

private ServiceConnection mConnection = new ServiceConnection() {
        @Override
        public void onServiceConnected(ComponentName className, IBinder binder) {
            mService = ((LoadArticlesService.LoadArticlesBinder) binder).getService();
        }
        public void onServiceDisconnected(ComponentName className) {
            mService = null;
        }
    };

onDestroy:

@Override
protected void onDestroy() {
    super.onDestroy();
    if (mService != null) {
        unbindService(mConnection);
    }
}

工作正常。


为什么要将服务设置为空? - IgorGanapolsky

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接