bindService源码流程

bindservice流程图.png

bindService(service, conn, flags)-->在Application/Activity中调用context.bindService,context都是Application/Activity的attch中传入的ContextImpl
Context-->bindService
ContextImpl-->bindService

< Context 的 bindService是怎么调到 ContextImpl中的bindService
首先 Activity/Service/Application 这些是继承ContextWrapper
ContextWrapper extends Context 会重写bindService方法,调用 mBase.bindService()
其中mBase就是ContextImpl (ContextImpl extends Context)>

private boolean bindServiceCommon(Intent service, ServiceConnection conn, int flags, String instanceName, Handler handler, Executor executor, UserHandle user) {
    IServiceConnection sd;
    ……
    // sd其实指向的是一个ServiceDispatch.InnerConnection的对象
    sd = mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
     ……
    //ActivityManager.getService()获取IActivityManager,可以直接向ActivityManagerService发送IPC消息
    //上面这个sd是为了跨进程回传信息用:最终通过这个对象调用ServiceConnection的onServiceConnected()方法   
    int res = ActivityManager.getService().bindIsolatedService(this.mMainThread.getApplicationThread(), 
       this.getActivityToken(), service, service.resolveTypeIfNeeded(this.getContentResolver()), sd, flags,
        instanceName,this.getOpPackageName(), user.getIdentifier());
    ……
}

这里面有两个比较重要的方法
1. 获取IServiceConnection实例 InnerConnection,<--下面会讲为什么
LoadedApk --> getServiceDispatcher-->getServiceDispatcherCommon

public final IServiceConnection getServiceDispatcher(ServiceConnection c,
        Context context, Executor executor, int flags) {
    return getServiceDispatcherCommon(c, context, null, executor, flags);
}

private IServiceConnection getServiceDispatcherCommon(ServiceConnection c,
        Context context, Handler handler, Executor executor, int flags) {
     ……
     LoadedApk.ServiceDispatcher sd = null;
     sd = new ServiceDispatcher(c, context, handler, flags);
     ……   
     return sd.getIServiceConnection();            
}

static final class ServiceDispatcher {
    private final ServiceDispatcher.InnerConnection mIServiceConnection;
    private static class InnerConnection extends IServiceConnection.Stub {
        ……
        public void connected(ComponentName name, IBinder service, boolean dead) throws RemoteException {
            LoadedApk.ServiceDispatcher sd = mDispatcher.get();
            if (sd != null) {
                sd.connected(name, service, dead);
            }
        }     
    }
    @UnsupportedAppUsage
    ServiceDispatcher(ServiceConnection conn,Context context, Handler activityThread, int flags) {
        mIServiceConnection = new InnerConnection(this);
        mConnection = conn;
        ……
    }

    @UnsupportedAppUsage
    IServiceConnection getIServiceConnection() {
        return mIServiceConnection;
    }
}

这里讲一下为什么说InnerConnection是IServiceConnection的实例
InnerConnection extends IServiceConnection.Stub
IServiceConnection.Stub extends Binder implements IServiceConnection
所以说InnerConnection是IServiceConnection的实例,看下面的代码

public interface IServiceConnection extends IInterface {
    @UnsupportedAppUsage
    void connected(ComponentName var1, IBinder var2, boolean var3) throws RemoteException;

    public abstract static class Stub extends Binder implements IServiceConnection {
        ……    
    }

2. 第二个重点方法 ActivityManagerService-->bindIsolatedService

public int bindIsolatedService(IApplicationThread caller, IBinder token, Intent service,
        String resolvedType, IServiceConnection connection, int flags, String instanceName,
        String callingPackage, int userId) throws TransactionTooLargeException {
      ……
      synchronized(this) {
          return mServices.bindServiceLocked(caller, token, service,
            resolvedType, connection, flags, instanceName, callingPackage, userId);
      }          
}

继续往下走,注意看代码里的注释
ActiveServices-->bindServiceLocked-->bringUpServiceLocked-->realStartServiceLocked

int bindServiceLocked(IApplicationThread caller, IBinder token, Intent service,
        String resolvedType, final IServiceConnection connection, int flags,
        String instanceName, String callingPackage, final int userId)
        throws TransactionTooLargeException {
   ……
   //根据用户传递进来Intent来检索相对应的服务
   ServiceLookupResult res =
    retrieveServiceLocked(service, instanceName, resolvedType, callingPackage,
            Binder.getCallingPid(), Binder.getCallingUid(), userId, true,
            callerFg, isBindExternal, allowInstant);
    ……
    ServiceRecord s = res.record;
    ……
    //创建AppBindRecord对象记录着当前ServiceRecord,intent以及发起方的进程信息
    AppBindRecord b = s.retrieveAppBindingLocked(service, callerApp);
    ……
    //如果bindService 的时候置上flag Context.BIND_AUTO_CREATE,那么会直接进入bringUpServiceLocked() 进行唤醒。
    if ((flags&Context.BIND_AUTO_CREATE) != 0) {
        s.lastActivity = SystemClock.uptimeMillis();
        //此方法中会判断是否首次bindService,如果是,则直接return 0;不会向下执行,
        //如果不是首次bind,则向下执行逻辑,直接回调connected
        if (bringUpServiceLocked(s, service.getFlags(), callerFg, false,
                permissionsReviewRequired) != null) {
            return 0;
        }
    }
    ……
    if (s.app != null && b.intent.received) {
        // Service is already running, so we can immediately
        // publish the connection.
        // c.conn就是上文中我们提到的ServiceDispatcher.InnerConnection的对象
        // 如果Service正在运行,通过此对象跨进程 调用他的connected()方法,此connected()中的逻辑下文会体现
        // (另外在下文中在首次bindService时publishServiceLocked中也会调用connected())
        try {
            c.conn.connected(s.name, b.intent.binder, false);
        } catch (Exception e) {
            Slog.w(TAG, "Failure sending service " + s.shortInstanceName
                + " to connection " + c.conn.asBinder()
                + " (in " + c.binding.client.processName + ")", e);
        }
    }
                    
}

private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,
        boolean whileRestarting, boolean permissionsReviewRequired)
        throws TransactionTooLargeException {
     //由本方法的参数可知,r是ServiceRecord的对象,ServiceRecord代表着一个Service记录
     //联系整个Service的启动过程可知:
     //首次调用bindService()启动一个Service时候,r.app应该为null,(是在realStartServiceLocked中赋值的)下面的if判断不成立
     // 如果不是首次调用bindService()方法,则下面的if判断成立,调用 sendServiceArgsLocked()方法,然后return     
     if (r.app != null && r.app.thread != null) {
        sendServiceArgsLocked(r, execInFg, false);
        return null;
     }    
     ……
     ProcessRecord app;

    if (!isolated) {
        app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);
        ……
        
        if (app != null && app.thread != null) {
            try {
                app.addPackage(r.appInfo.packageName, r.appInfo.longVersionCode, mAm.mProcessStats);
                realStartServiceLocked(r, app, execInFg);
                return null;
            } catch (TransactionTooLargeException e) {
                throw e;
            } catch (RemoteException e) {
                Slog.w(TAG, "Exception when starting service " + r.shortInstanceName, e);
            }

            // If a dead object exception was thrown -- fall through to
            // restart the application.
        }
       ……         
                                      
}

realStartServiceLocked中有两个比较重要的方法
realStartServiceLocked-->app.thread.scheduleCreateService
realStartServiceLocked-->requestServiceBindingsLocked

private final void realStartServiceLocked(ServiceRecord r,
        ProcessRecord app, boolean execInFg) throws RemoteException {
   ……
   //在此方法中给r.app赋值         
   r.setProcess(app);         
   ……
   //app.thread其实就是ActivityThread中ApplicationThread类的对象,
   //调用ApplicationThread的scheduleCreateService()方法
   //其实该方法就最终会调用到Service的onCreate()方法 
   app.thread.scheduleCreateService(r, r.serviceInfo,
        mAm.compatibilityInfoForPackage(r.serviceInfo.applicationInfo),
        app.getReportedProcState());
   ……
   //该方法是通过bindService()绑定Service的时候才去真正的调用,从而调用onBind()方法
   //当通过startService()方法来开启一个Service的时候,该方法内部的逻辑不成立
   //稍后会介绍这个方法的内容
   requestServiceBindingsLocked(r, execInFg);
   ……                             
}

1. realStartServiceLocked-->app.thread.scheduleCreateService

public final void scheduleCreateService(IBinder token,
        ServiceInfo info, CompatibilityInfo compatInfo, int processState) {
    updateProcessState(processState, false);
    CreateServiceData s = new CreateServiceData();
    s.token = token;
    s.info = info;
    s.compatInfo = compatInfo;

    sendMessage(H.CREATE_SERVICE, s);
}

H处理message

public void handleMessage(Message msg) {
    ……
    switch (msg.what) {
        ……
        case CREATE_SERVICE:
        ……
        handleCreateService((CreateServiceData)msg.obj);
        ……
        break;
            ……
      }
      ……
 }
 
 private void handleCreateService(CreateServiceData data) {
     ……
    Service service = null;
    try {
        ……
    
        ContextImpl context = ContextImpl.createAppContext(this, packageInfo);
        Application app = packageInfo.makeApplication(false, mInstrumentation);
        java.lang.ClassLoader cl = packageInfo.getClassLoader();
        service = packageInfo.getAppFactory().instantiateService(cl, data.info.name, data.intent);
        ……
        //这里把ContextImpl传递进service
        service.attach(context, this, data.info.name, data.token, app, ActivityManager.getService());
        service.onCreate();
       ……
    } catch (Exception e) {
        ……
    }
    ……
 }

2. realStartServiceLocked-->requestServiceBindingsLocked
requestServiceBindingsLocked->requestServiceBindingLocked-->r.app.thread.scheduleBindService

private final void requestServiceBindingsLocked(ServiceRecord r, boolean execInFg)
        throws TransactionTooLargeException {
    for (int i=r.bindings.size()-1; i>=0; i--) {
        IntentBindRecord ibr = r.bindings.valueAt(i);
        if (!requestServiceBindingLocked(r, ibr, execInFg, false)) {
            break;
        }
    }
}

private final boolean requestServiceBindingLocked(ServiceRecord r, IntentBindRecord i,
        boolean execInFg, boolean rebind) throws TransactionTooLargeException {
    if (r.app == null || r.app.thread == null) {
        // If service is not currently running, can't yet bind.
        return false;
    }
    ……  
       //细节,在这个方法中会有调用scheduleServiceTimeoutLocked 这个是ANR超时逻辑,前台服务20s,后台200s
       //然后会在在绑定成功后调用ActiveServices中的serviceDoneExecutingLocked解除ANR监听,下文有体现
       bumpServiceExecutingLocked(r, execInFg, "bind");
       //app.thread其实就是ActivityThread中ApplicationThread类的对象,
       //调用ApplicationThread的scheduleBindService()方法      
       r.app.thread.scheduleBindService(r, i.intent.getIntent(), rebind,r.app.getReportedProcState());  
    ……  
    return true;                   
}

private final void bumpServiceExecutingLocked(ServiceRecord r, boolean fg, String why) {4
    ……
        scheduleServiceTimeoutLocked(r.app);
    ……    
}
void scheduleServiceTimeoutLocked(ProcessRecord proc) {
    if (proc.executingServices.size() == 0 || proc.thread == null) {
        return;
    }
    Message msg = mAm.mHandler.obtainMessage(
            ActivityManagerService.SERVICE_TIMEOUT_MSG);
    msg.obj = proc;
    //这个是ANR超时逻辑,前台服务20s,后台200s
    mAm.mHandler.sendMessageDelayed(msg, proc.execServicesFg ? SERVICE_TIMEOUT : SERVICE_BACKGROUND_TIMEOUT);
}

继续往下
ApplicationThread->scheduleBindService-->sendMessage(H.BIND_SERVICE, s)

public final void scheduleBindService(IBinder token, Intent intent,
        boolean rebind, int processState) {
    ……
    sendMessage(H.BIND_SERVICE, s);
}

H处理message -->handleBindService--> ActivityManager.getService().publishService

public void handleMessage(Message msg) {
    ……
    switch (msg.what) {
        ……
        case BIND_SERVICE:
            ……
            handleBindService((BindServiceData)msg.obj);
            ……
            break;
            ……
      }
      ……
 }
 private void handleBindService(BindServiceData data) {
    Service s = mServices.get(data.token);
    ……
    if (s != null) {
        try {
            ……
            try {
                if (!data.rebind) {
                    IBinder binder = s.onBind(data.intent);
                    ActivityManager.getService().publishService(data.token, data.intent, binder);
                } else {
                    s.onRebind(data.intent);
                    //在绑定成功后解除ANR监听
                    //最终会调到 ActiveServices中的serviceDoneExecutingLocked
                    ActivityManager.getService().serviceDoneExecuting(
                            data.token, SERVICE_DONE_EXECUTING_ANON, 0, 0);
                }
            } catch (RemoteException ex) {
               ……
            }
        } catch (Exception e) {
            ……
        }
    }
}

ActivityManagerService->publishService

public void publishService(IBinder token, Intent intent, IBinder service) {
   ……
    synchronized(this) {
        ……
        mServices.publishServiceLocked((ServiceRecord)token, intent, service);
    }
}

ActiveServices->publishServiceLocked

void publishServiceLocked(ServiceRecord r, Intent intent, IBinder service) {
    ……
    //数据connections是在bindServiceLocked中添加的
    ArrayMap<IBinder, ArrayList<ConnectionRecord>> connections = r.getConnections();
    for (int conni = connections.size() - 1; conni >= 0; conni--) {
        ArrayList<ConnectionRecord> clist = connections.valueAt(conni);
        for (int i=0; i<clist.size(); i++) {
            ConnectionRecord c = clist.get(i);
             ……
            try {
                //c.conn就是上文中我们提到的ServiceDispatcher.InnerConnection的对象,
                //通过此对象进行跨进程调用 ServiceConnection的onServiceConnected(ComponentName name, IBinder service)
                //在上文中的bindServiceLocked也调用过
                c.conn.connected(r.name, service, false);
            } catch (Exception e) {
                ……
            }
         }
    }
   
}

回顾一下 ServiceDispatcher.InnerConnection的创建过程
ContextImpl-->bindServiceCommon-->mPackageInfo.getServiceDispatcher(conn, getOuterContext(), handler, flags);
mPackageInfo就是 LoadedApk
LoadedApk-->getServiceDispatcher-->getServiceDispatcherCommon-->IServiceConnection的实例ServiceDispatcher.InnerConnection
所以
c.conn.connected-->InnerConnection中的 connected(ComponentName name, IBinder service, boolean dead)

public void connected(ComponentName name, IBinder service, boolean dead)
        throws RemoteException {
    LoadedApk.ServiceDispatcher sd = mDispatcher.get();
    if (sd != null) {
        sd.connected(name, service, dead);
    }
}

LoadedApk.ServiceDispatcher-->connected-->doConnected

public void connected(ComponentName name, IBinder service, boolean dead) {
    //RunConnection()中最终也会调用到doConnected中
    if (mActivityExecutor != null) {
        mActivityExecutor.execute(new RunConnection(name, service, 0, dead));
    } else if (mActivityThread != null) {
        mActivityThread.post(new RunConnection(name, service, 0, dead));
    } else {
        doConnected(name, service, dead);
    }
}
private final ServiceConnection mConnection;
public void doConnected(ComponentName name, IBinder service, boolean dead) {
    ……
    // If there was an old service, it is now disconnected.
    //如果不为null,取消和旧的Service的连接
    if (old != null) {
        mConnection.onServiceDisconnected(name);
    }
    //根据上文传值dead为false
    if (dead) {
        mConnection.onBindingDied(name);
    }
    // If there is a new viable service, it is now connected.
    // 建立新的连接
    if (service != null) {
        mConnection.onServiceConnected(name, service);
    } else {
        // The binding machinery worked, but the remote returned null from onBind().
        mConnection.onNullBinding(name);
    }
}

mConnection.onServiceConnected(name, service);
此方法就是bindservice中的回调方法
至此bindService流程完事。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 206,214评论 6 481
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 88,307评论 2 382
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 152,543评论 0 341
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 55,221评论 1 279
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 64,224评论 5 371
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 49,007评论 1 284
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 38,313评论 3 399
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,956评论 0 259
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 43,441评论 1 300
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,925评论 2 323
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 38,018评论 1 333
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,685评论 4 322
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 39,234评论 3 307
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 30,240评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 31,464评论 1 261
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 45,467评论 2 352
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,762评论 2 345