BIND_AUTO_CREATE的问题(转)

转自:http://xiaoyaozjl.iteye.com/blog/2156335

先看下面两段非常简单的代码,功能是通过一个Activity启动并绑定一个本地服务,然后马上调用停止服务

package com.example.servicetest2;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;

public class MainActivity extends ActionBarActivity {

    private static final String LOG_TAG = "MainActivity";

    private ServiceConnection conn = new TestConnection();

    private Intent i;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        i = new Intent(this, TestService.class);
        startService(i);// 新建服务
        bindService(i, conn, 0);// 该Activity bind到该服务,0表示不新建服务
    }

    protected void onConnected() {
        stopService(i);// bind到服务后,马上stop掉
    }

    private class TestConnection implements ServiceConnection {

        @Override
        public void onServiceConnected(final ComponentName name, final IBinder service) {
            Log.d(LOG_TAG, "onServiceConnected");
            onConnected();
        }

        @Override
        public void onServiceDisconnected(final ComponentName name) {
            Log.d(LOG_TAG, "onServiceDisconnected");
        }

    }

}

TestService.java

package com.example.servicetest2;

import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.util.Log;

public class TestService extends Service {

    private static final String LOG_TAG = "TestService";

    @Override
    public void onCreate() {
        Log.d(LOG_TAG, "onCreate");
    }

    @Override
    public IBinder onBind(final Intent intent) {
        Log.d(LOG_TAG, "onBind");
        return new TestBinder();
    }

    @Override
    public boolean onUnbind(final Intent intent) {
        Log.d(LOG_TAG, "onUnbind");
        return false;
    }

    @Override
    public void onDestroy() {
        Log.d(LOG_TAG, "onDestroy");
    }

    public static class TestBinder extends Binder {}

}

按照安卓官方文档(http://developer.android.com/guide/components/services.htmlhttp://developer.android.com/guide/components/bound-services.html)的说法,如果一个服务既被startService启动又被其他组件bind到,那么调用stopService时该服务不会被销毁,直到所有的client都unbind。事实是否如此呢?上述代码执行结果如下:

11-14 22:18:17.507: D/TestService(2481): onCreate
11-14 22:18:17.527: D/TestService(2481): onBind
11-14 22:18:17.547: D/MainActivity(2481): onServiceConnected
11-14 22:18:17.607: D/MainActivity(2481): onServiceDisconnected
11-14 22:18:17.607: D/TestService(2481): onUnbind
11-14 22:18:17.607: D/TestService(2481): onDestroy

可以看到虽然在代码中并未unbind该服务,但当调用stopService时服务马上就被销毁,而且触发了onServiceDisconnected回调,明显和官方文档以及网上各种说法不一致。为什么会这样呢?去翻关闭服务的源代码:

 

ActivityManagerService.java

private final void bringDownServiceLocked(final ServiceRecord r, final boolean force) {
        // Slog.i(TAG, "Bring down service:");
        // r.dump(" ");
        // Does it still need to run?
        if (!force &&
            r.startRequested) {// 检查stopService或stopSelf是否被调用
            return;
        }
        if (r.connections.size() > 0) {
            if (!force) {
                // XXX should probably keep a count of the number of auto-create
                // connections directly in the service.
                final Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator();
                while (it.hasNext()) {// 遍历所有绑定到服务的连接记录
                    final ArrayList<ConnectionRecord> cr = it.next();
                    for (int i = 0; i < cr.size(); i++) {
                        if ((cr.get(i).flags & Context.BIND_AUTO_CREATE) != 0) {// 检查连接是否设置了BIND_AUTO_CREATE
                            return;// 如果存在设置了BIND_AUTO_CREATE的连接,那么就不销毁服务直接返回
                        }
                    }
                }
            }
            // Report to all of the connections that the service is no longer
            // available.
            final Iterator<ArrayList<ConnectionRecord>> it = r.connections.values().iterator();
            while (it.hasNext()) {
                final ArrayList<ConnectionRecord> c = it.next();
                for (int i = 0; i < c.size(); i++) {
                    final ConnectionRecord cr = c.get(i);
                    // There is still a connection to the service that is
                    // being brought down. Mark it as dead.
                    cr.serviceDead = true;
                    try {
                        cr.conn.connected(r.name, null);// 回调ServiceConnection.onServiceDisconnected
                    } catch (final Exception e) {
                        Slog.w(TAG, "Failure disconnecting service " +
                                    r.name +
                                    " to connection " + c.get(i).conn.asBinder() +
                                    " (in " + c.get(i).binding.client.processName + ")", e);
                    }
                }
            }

        }

        // ...
        // 销毁服务
    }

ActivityManagerService.bringDownServiceLocked方法负责销毁服务,无论stopService或unbindService最终都可能会调用该方法。可以从代码中看到,在真正销毁服务前,会检查和该服务绑定的连接信息(调用该次unbindService的连接在前面已经被过滤掉),如果扔有设置过BIND_AUTO_CREATE的链接存在,就不进行销毁。换句话说,一个BoundService是否被销毁,取决于当前带有BIND_AUTO_CREATE标志的连接数目,不带有BIND_AUTO_CREATE标志的连接会在服务销毁前收到onServiceDisconnected回调。

 

个人认为,销毁服务的代码实现逻辑和开发设想是有出入的,本应该有特殊的标志位决定是否在有client绑定的情况下销毁服务而不是简单粗暴的“重用”BIND_AUTO_CREATE标志。不过BIND_AUTO_CREATE和startService同时使用并无副作用(同名服务在安卓里头是单例),我们可以根据业务需求灵活设置bindService方法的flags参数。比如一个多client连接的短时间执行的共享服务,当client指定bindService的flags为0时,服务在执行完毕可以及时stopSelf销毁,而不需要等待client unbind,可以有效的节省资源。

 

    原文作者:ActivityManagerService
    原文地址: http://www.cnblogs.com/pengyingh/articles/4844025.html
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞