我的应用程序使用其他(我的)应用程序提供的服务。我正在使用bound serviceMessenger来访问它并与之通信(由于它是一个不同的应用程序,因此它也是一个远程服务)。

当我以适当的 Intent 调用bindService时,该调用返回false(如不提供服务的APK所期望的那样),我从文档和示例代码中假设,我不需要unbind ServiceConnection。但是,在我的Galaxy Nexus(Jelly Bean)设备上执行此操作时,在完成ServiceConnectionLeaked时会收到众所周知的Activity消息。

我已经通过这样做来挽救了

if (!ctxt.bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE)) {
    try {
        ctxt.unbindService(serviceConnection);
    } catch (Throwable t) {}
    // Clean up
    return;
}
// Store serviceConnection for future use

我很好奇:我只是错过了文档中的某些内容吗,应该以这种方式工作吗?我添加了try ... catch以确保即使在其他设备或Android版本上此行为确实有所不同,我的应用也不会受到(负面)影响。

最佳答案

一般而言,ServiceConnection始终由框架分配和注册,而不管bindService()调用返回true还是false。请参见android.app.ContextImpl中的bindService()实现:

public boolean bindService(Intent service, ServiceConnection conn, int flags, int userHandle) {
    IServiceConnection sd;
    if (conn == null) {
        throw new IllegalArgumentException("connection is null");
    }
    if (mPackageInfo != null) {
        // A new ServiceDispatcher will be created and registered along with
        // ServiceConnection in LoadedApk.mService for your application context.
        sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(),
                mMainThread.getHandler(), flags);
    } else {
        throw new RuntimeException("Not supported in system context");
    }
    try {
        ... ...
        return res != 0;
    } catch (RemoteException e) {
        return false;
    }
}
正如the official dev guide所建议的那样,在使用完服务后,应始终取消绑定(bind)该服务,这是一种很好的编程方式:

当框架开始执行最终清理(例如,当您的应用程序退出时)并发现有未注册的ServiceConnection时,将引发ServiceConnectionLeaked,然后框架将尝试为您解除绑定(bind)。请参见android.app.LoadedApk中的removeContextRegistrations()实现:
public void removeContextRegistrations(Context context,
        String who, String what) {
    final boolean reportRegistrationLeaks = StrictMode.vmRegistrationLeaksEnabled();
    ... ...
    //Slog.i(TAG, "Receiver registrations: " + mReceivers);
    HashMap<ServiceConnection, LoadedApk.ServiceDispatcher> smap =
        mServices.remove(context);
    if (smap != null) {
        Iterator<LoadedApk.ServiceDispatcher> it = smap.values().iterator();
        while (it.hasNext()) {
            LoadedApk.ServiceDispatcher sd = it.next();
            ServiceConnectionLeaked leak = new ServiceConnectionLeaked(
                    what + " " + who + " has leaked ServiceConnection "
                    + sd.getServiceConnection() + " that was originally bound here");
            leak.setStackTrace(sd.getLocation().getStackTrace());
            Slog.e(ActivityThread.TAG, leak.getMessage(), leak);
            if (reportRegistrationLeaks) {
                StrictMode.onServiceConnectionLeaked(leak);
            }
            try {
                ActivityManagerNative.getDefault().unbindService(
                        sd.getIServiceConnection());
            } catch (RemoteException e) {
                // system crashed, nothing we can do
            }
            sd.doForget();
        }
    }
    mUnboundServices.remove(context);
    //Slog.i(TAG, "Service registrations: " + mServices);
}

10-08 03:45