调用startservice()进入ContextWrapper类的startService方法

这里mBase是一个Context,即调用Context类的startservice(),而context是一个抽象类,他的实现类是contextImpl
因此我们去看contextImpl类的startservice()

private ComponentName startServiceCommon(Intent service, boolean requireForeground,UserHandle user) {try {validateServiceIntent(service);service.prepareToLeaveProcess(this);ComponentName cn = ActivityManager.getService().startService(mMainThread.getApplicationThread(), service,service.resolveTypeIfNeeded(getContentResolver()), requireForeground,getOpPackageName(), getAttributionTag(), user.getIdentifier());if (cn != null) {if (cn.getPackageName().equals("!")) {throw new SecurityException("Not allowed to start service " + service+ " without permission " + cn.getClassName());} else if (cn.getPackageName().equals("!!")) {throw new SecurityException("Unable to start service " + service+ ": " + cn.getClassName());} else if (cn.getPackageName().equals("?")) {throw new IllegalStateException("Not allowed to start service " + service + ": " + cn.getClassName());}}return cn;} catch (RemoteException e) {throw e.rethrowFromSystemServer();}}

在ActivityManagerService中,启动service的操作 是委托给ActiveServices执行的。
ActiveServices类的startServiceLocked

    ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,int callingPid, int callingUid, boolean fgRequired, String callingPackage,@Nullable String callingFeatureId, final int userId,boolean allowBackgroundActivityStarts) throws TransactionTooLargeException {if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "startService: " + service+ " type=" + resolvedType + " args=" + service.getExtras());final boolean callerFg;if (caller != null) {final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller);if (callerApp == null) {throw new SecurityException("Unable to find app for caller " + caller+ " (pid=" + callingPid+ ") when starting service " + service);}callerFg = callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND;} else {callerFg = true;}//(1)解析、创建响应的ServiceRecordServiceLookupResult res =retrieveServiceLocked(service, null, resolvedType, callingPackage,callingPid, callingUid, userId, true, callerFg, false, false);if (res == null) {return null;}if (res.record == null) {return new ComponentName("!", res.permission != null? res.permission : "private to package");}ServiceRecord r = res.record;if (!mAm.mUserController.exists(r.userId)) {Slog.w(TAG, "Trying to start service with non-existent user! " + r.userId);return null;}// If we're starting indirectly (e.g. from PendingIntent), figure out whether// we're launching into an app in a background state.  This keys off of the same// idleness state tracking as e.g. O+ background service start policy.final boolean bgLaunch = !mAm.isUidActiveLocked(r.appInfo.uid);// If the app has strict background restrictions, we treat any bg service// start analogously to the legacy-app forced-restrictions case, regardless// of its target SDK version.boolean forcedStandby = false;if (bgLaunch && appRestrictedAnyInBackground(r.appInfo.uid, r.packageName)) {if (DEBUG_FOREGROUND_SERVICE) {Slog.d(TAG, "Forcing bg-only service start only for " + r.shortInstanceName+ " : bgLaunch=" + bgLaunch + " callerFg=" + callerFg);}forcedStandby = true;}// If this is a direct-to-foreground start, make sure it is allowed as per the app op.boolean forceSilentAbort = false;if (fgRequired) {final int mode = mAm.getAppOpsManager().checkOpNoThrow(AppOpsManager.OP_START_FOREGROUND, r.appInfo.uid, r.packageName);switch (mode) {case AppOpsManager.MODE_ALLOWED:case AppOpsManager.MODE_DEFAULT:// All okay.break;case AppOpsManager.MODE_IGNORED:// Not allowed, fall back to normal start service, failing siliently// if background check restricts that.Slog.w(TAG, "startForegroundService not allowed due to app op: service "+ service + " to " + r.shortInstanceName+ " from pid=" + callingPid + " uid=" + callingUid+ " pkg=" + callingPackage);fgRequired = false;forceSilentAbort = true;break;default:return new ComponentName("!!", "foreground not allowed as per app op");}}// If this isn't a direct-to-foreground start, check our ability to kick off an// arbitrary serviceif (forcedStandby || (!r.startRequested && !fgRequired)) {// Before going further -- if this app is not allowed to start services in the// background, then at this point we aren't going to let it period.final int allowed = mAm.getAppStartModeLocked(r.appInfo.uid, r.packageName,r.appInfo.targetSdkVersion, callingPid, false, false, forcedStandby);if (allowed != ActivityManager.APP_START_MODE_NORMAL) {Slog.w(TAG, "Background start not allowed: service "+ service + " to " + r.shortInstanceName+ " from pid=" + callingPid + " uid=" + callingUid+ " pkg=" + callingPackage + " startFg?=" + fgRequired);if (allowed == ActivityManager.APP_START_MODE_DELAYED || forceSilentAbort) {// In this case we are silently disabling the app, to disrupt as// little as possible existing apps.return null;}if (forcedStandby) {// This is an O+ app, but we might be here because the user has placed// it under strict background restrictions.  Don't punish the app if it's// trying to do the right thing but we're denying it for that reason.if (fgRequired) {if (DEBUG_BACKGROUND_CHECK) {Slog.v(TAG, "Silently dropping foreground service launch due to FAS");}return null;}}// This app knows it is in the new model where this operation is not// allowed, so tell it what has happened.UidRecord uidRec = mAm.mProcessList.getUidRecordLocked(r.appInfo.uid);return new ComponentName("?", "app is in background uid " + uidRec);}}// At this point we've applied allowed-to-start policy based on whether this was// an ordinary startService() or a startForegroundService().  Now, only require that// the app follow through on the startForegroundService() -> startForeground()// contract if it actually targets O+.if (r.appInfo.targetSdkVersion < Build.VERSION_CODES.O && fgRequired) {if (DEBUG_BACKGROUND_CHECK || DEBUG_FOREGROUND_SERVICE) {Slog.i(TAG, "startForegroundService() but host targets "+ r.appInfo.targetSdkVersion + " - not requiring startForeground()");}fgRequired = false;}NeededUriGrants neededGrants = mAm.mUgmInternal.checkGrantUriPermissionFromIntent(service, callingUid, r.packageName, r.userId);// If permissions need a review before any of the app components can run,// we do not start the service and launch a review activity if the calling app// is in the foreground passing it a pending intent to start the service when// review is completed.// XXX This is not dealing with fgRequired!if (!requestStartTargetPermissionsReviewIfNeededLocked(r, callingPackage, callingFeatureId,callingUid, service, callerFg, userId)) {return null;}if (unscheduleServiceRestartLocked(r, callingUid, false)) {if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "START SERVICE WHILE RESTART PENDING: " + r);}r.lastActivity = SystemClock.uptimeMillis();r.startRequested = true;r.delayedStop = false;r.fgRequired = fgRequired;r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),service, neededGrants, callingUid));if (fgRequired) {// We are now effectively running a foreground service.ServiceState stracker = r.getTracker();if (stracker != null) {stracker.setForeground(true, mAm.mProcessStats.getMemFactorLocked(),r.lastActivity);}mAm.mAppOpsService.startOperation(AppOpsManager.getToken(mAm.mAppOpsService),AppOpsManager.OP_START_FOREGROUND, r.appInfo.uid, r.packageName, null,true, false, null, false);}final ServiceMap smap = getServiceMapLocked(r.userId);boolean addToStarting = false;if (!callerFg && !fgRequired && r.app == null&& mAm.mUserController.hasStartedUserState(r.userId)) {ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false);if (proc == null || proc.getCurProcState() > ActivityManager.PROCESS_STATE_RECEIVER) {// If this is not coming from a foreground caller, then we may want// to delay the start if there are already other background services// that are starting.  This is to avoid process start spam when lots// of applications are all handling things like connectivity broadcasts.// We only do this for cached processes, because otherwise an application// can have assumptions about calling startService() for a service to run// in its own process, and for that process to not be killed before the// service is started.  This is especially the case for receivers, which// may start a service in onReceive() to do some additional work and have// initialized some global state as part of that.if (DEBUG_DELAYED_SERVICE) Slog.v(TAG_SERVICE, "Potential start delay of "+ r + " in " + proc);if (r.delayed) {// This service is already scheduled for a delayed start; just leave// it still waiting.if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Continuing to delay: " + r);return r.name;}if (smap.mStartingBackground.size() >= mMaxStartingBackground) {// Something else is starting, delay!Slog.i(TAG_SERVICE, "Delaying start of: " + r);smap.mDelayedStartList.add(r);r.delayed = true;return r.name;}if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Not delaying: " + r);addToStarting = true;} else if (proc.getCurProcState() >= ActivityManager.PROCESS_STATE_SERVICE) {// We slightly loosen when we will enqueue this new service as a background// starting service we are waiting for, to also include processes that are// currently running other services or receivers.addToStarting = true;if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE,"Not delaying, but counting as bg: " + r);} else if (DEBUG_DELAYED_STARTS) {StringBuilder sb = new StringBuilder(128);sb.append("Not potential delay (state=").append(proc.getCurProcState()).append(' ').append(proc.adjType);String reason = proc.makeAdjReason();if (reason != null) {sb.append(' ');sb.append(reason);}sb.append("): ");sb.append(r.toString());Slog.v(TAG_SERVICE, sb.toString());}} else if (DEBUG_DELAYED_STARTS) {if (callerFg || fgRequired) {Slog.v(TAG_SERVICE, "Not potential delay (callerFg=" + callerFg + " uid="+ callingUid + " pid=" + callingPid + " fgRequired=" + fgRequired + "): " + r);} else if (r.app != null) {Slog.v(TAG_SERVICE, "Not potential delay (cur app=" + r.app + "): " + r);} else {Slog.v(TAG_SERVICE,"Not potential delay (user " + r.userId + " not started): " + r);}}if (allowBackgroundActivityStarts) {r.whitelistBgActivityStartsOnServiceStart();}**//(2)调用内部方法**ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);if (!r.mAllowWhileInUsePermissionInFgs) {r.mAllowWhileInUsePermissionInFgs =shouldAllowWhileInUsePermissionInFgsLocked(callingPackage, callingPid,callingUid, service, r, allowBackgroundActivityStarts);}return cmp;}
 ComponentName startServiceInnerLocked(ServiceMap smap, Intent service, ServiceRecord r,boolean callerFg, boolean addToStarting) throws TransactionTooLargeException {ServiceState stracker = r.getTracker();if (stracker != null) {stracker.setStarted(true, mAm.mProcessStats.getMemFactorLocked(), r.lastActivity);}r.callStart = false;FrameworkStatsLog.write(FrameworkStatsLog.SERVICE_STATE_CHANGED, r.appInfo.uid,r.name.getPackageName(), r.name.getClassName(),FrameworkStatsLog.SERVICE_STATE_CHANGED__STATE__START);synchronized (r.stats.getBatteryStats()) {r.stats.startRunningLocked();}String error = bringUpServiceLocked(r, service.getFlags(), callerFg, false, false);if (error != null) {return new ComponentName("!!", error);}if (r.startRequested && addToStarting) {boolean first = smap.mStartingBackground.size() == 0;smap.mStartingBackground.add(r);r.startingBgTimeout = SystemClock.uptimeMillis() + mAm.mConstants.BG_START_TIMEOUT;if (DEBUG_DELAYED_SERVICE) {RuntimeException here = new RuntimeException("here");here.fillInStackTrace();Slog.v(TAG_SERVICE, "Starting background (first=" + first + "): " + r, here);} else if (DEBUG_DELAYED_STARTS) {Slog.v(TAG_SERVICE, "Starting background (first=" + first + "): " + r);}if (first) {smap.rescheduleDelayedStartsLocked();}} else if (callerFg || r.fgRequired) {smap.ensureNotStartingBackgroundLocked(r);}return r.name;}
    private String bringUpServiceLocked(ServiceRecord r, int intentFlags, boolean execInFg,boolean whileRestarting, boolean permissionsReviewRequired)throws TransactionTooLargeException {**//注释一:如果Service已经创建,则直接调用sendServiceArgsLocked(),最终会触发Service.onStartCommand()方法调用**//  r 是serviceRecordif (r.app != null && r.app.thread != null) {sendServiceArgsLocked(r, execInFg, false);return null;}if (!whileRestarting && mRestartingServices.contains(r)) {// If waiting for a restart, then do nothing.return null;}if (DEBUG_SERVICE) {Slog.v(TAG_SERVICE, "Bringing up " + r + " " + r.intent + " fg=" + r.fgRequired);}// We are now bringing the service up, so no longer in the// restarting state.if (mRestartingServices.remove(r)) {clearRestartingIfNeededLocked(r);}// Make sure this service is no longer considered delayed, we are starting it now.if (r.delayed) {if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "REM FR DELAY LIST (bring up): " + r);getServiceMapLocked(r.userId).mDelayedStartList.remove(r);r.delayed = false;}// Make sure that the user who owns this service is started.  If not,// we don't want to allow it to run.if (!mAm.mUserController.hasStartedUserState(r.userId)) {String msg = "Unable to launch app "+ r.appInfo.packageName + "/"+ r.appInfo.uid + " for service "+ r.intent.getIntent() + ": user " + r.userId + " is stopped";Slog.w(TAG, msg);bringDownServiceLocked(r);return msg;}// Service is now being launched, its package can't be stopped.try {AppGlobals.getPackageManager().setPackageStoppedState(r.packageName, false, r.userId);} catch (RemoteException e) {} catch (IllegalArgumentException e) {Slog.w(TAG, "Failed trying to unstop package "+ r.packageName + ": " + e);}final boolean isolated = (r.serviceInfo.flags&ServiceInfo.FLAG_ISOLATED_PROCESS) != 0;final String procName = r.processName;HostingRecord hostingRecord = new HostingRecord("service", r.instanceName);ProcessRecord app;if (!isolated) {app = mAm.getProcessRecordLocked(procName, r.appInfo.uid, false);if (DEBUG_MU) Slog.v(TAG_MU, "bringUpServiceLocked: appInfo.uid=" + r.appInfo.uid+ " app=" + app);if (app != null && app.thread != null) {try {app.addPackage(r.appInfo.packageName, r.appInfo.longVersionCode, mAm.mProcessStats);//注释二:非独立进程,则直接启动Service:onCreate(),onStartCommad()方法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.}} else {// If this service runs in an isolated process, then each time// we call startProcessLocked() we will get a new isolated// process, starting another process if we are currently waiting// for a previous process to come up.  To deal with this, we store// in the service any current isolated process it is running in or// waiting to have come up.app = r.isolatedProc;if (WebViewZygote.isMultiprocessEnabled()&& r.serviceInfo.packageName.equals(WebViewZygote.getPackageName())) {hostingRecord = HostingRecord.byWebviewZygote(r.instanceName);}if ((r.serviceInfo.flags & ServiceInfo.FLAG_USE_APP_ZYGOTE) != 0) {hostingRecord = HostingRecord.byAppZygote(r.instanceName, r.definingPackageName,r.definingUid);}}// Not running -- get it started, and enqueue this service record// to be executed when the app comes up.//注释三: 如果Service要运行独立进程,且进程不存在,则创建新进程.      if (app == null && !permissionsReviewRequired) {// TODO (chriswailes): Change the Zygote policy flags based on if the launch-for-service//  was initiated from a notification tap or not.if ((app=mAm.startProcessLocked(procName, r.appInfo, true, intentFlags,hostingRecord, ZYGOTE_POLICY_FLAG_EMPTY, false, isolated, false)) == null) {String msg = "Unable to launch app "+ r.appInfo.packageName + "/"+ r.appInfo.uid + " for service "+ r.intent.getIntent() + ": process is bad";Slog.w(TAG, msg);bringDownServiceLocked(r);return msg;}if (isolated) {r.isolatedProc = app;}}if (r.fgRequired) {if (DEBUG_FOREGROUND_SERVICE) {Slog.v(TAG, "Whitelisting " + UserHandle.formatUid(r.appInfo.uid)+ " for fg-service launch");}mAm.tempWhitelistUidLocked(r.appInfo.uid,SERVICE_START_FOREGROUND_TIMEOUT, "fg-service-launch");}if (!mPendingServices.contains(r)) {mPendingServices.add(r);}if (r.delayedStop) {// Oh and hey we've already been asked to stop!r.delayedStop = false;if (r.startRequested) {if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE,"Applying delayed stop (in bring up): " + r);stopServiceLocked(r);}}return null;}

下面我们一起来看一下ActiveServices是 如何创建,并启动Service的。
核心方法在realStartServiceLocked()

    private final void realStartServiceLocked(ServiceRecord r,ProcessRecord app, boolean execInFg) throws RemoteException {if (app.thread == null) {throw new RemoteException();}if (DEBUG_MU)Slog.v(TAG_MU, "realStartServiceLocked, ServiceRecord.uid = " + r.appInfo.uid+ ", ProcessRecord.uid = " + app.uid);r.setProcess(app);r.restartTime = r.lastActivity = SystemClock.uptimeMillis();final boolean newService = app.startService(r);bumpServiceExecutingLocked(r, execInFg, "create");mAm.updateLruProcessLocked(app, false, null);updateServiceForegroundLocked(r.app, /* oomAdj= */ false);mAm.updateOomAdjLocked(app, OomAdjuster.OOM_ADJ_REASON_START_SERVICE);boolean created = false;try {if (LOG_SERVICE_START_STOP) {String nameTerm;int lastPeriod = r.shortInstanceName.lastIndexOf('.');nameTerm = lastPeriod >= 0 ? r.shortInstanceName.substring(lastPeriod): r.shortInstanceName;EventLogTags.writeAmCreateService(r.userId, System.identityHashCode(r), nameTerm, r.app.uid, r.app.pid);}FrameworkStatsLog.write(FrameworkStatsLog.SERVICE_LAUNCH_REPORTED, r.appInfo.uid,r.name.getPackageName(), r.name.getClassName());synchronized (r.stats.getBatteryStats()) {r.stats.startLaunchedLocked();}mAm.notifyPackageUse(r.serviceInfo.packageName,PackageManager.NOTIFY_PACKAGE_USE_SERVICE);app.forceProcessStateUpTo(ActivityManager.PROCESS_STATE_SERVICE);//(1)向ApplicationThread发送消息,创建Serviceapp.thread.scheduleCreateService(r, r.serviceInfo,mAm.compatibilityInfoForPackage(r.serviceInfo.applicationInfo),app.getReportedProcState());r.postNotification();created = true;} catch (DeadObjectException e) {Slog.w(TAG, "Application dead when creating service " + r);mAm.appDiedLocked(app, "Died when creating service");throw e;} finally {if (!created) {// Keep the executeNesting count accurate.final boolean inDestroying = mDestroyingServices.contains(r);serviceDoneExecutingLocked(r, inDestroying, inDestroying);// Cleanup.if (newService) {app.stopService(r);r.setProcess(null);}// Retry.if (!inDestroying) {scheduleServiceRestartLocked(r, false);}}}if (r.whitelistManager) {app.whitelistManager = true;}requestServiceBindingsLocked(r, execInFg);updateServiceClientActivitiesLocked(app, null, true);if (newService && created) {app.addBoundClientUidsOfNewService(r);}// If the service is in the started state, and there are no// pending arguments, then fake up one so its onStartCommand() will// be called.if (r.startRequested && r.callStart && r.pendingStarts.size() == 0) {r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),null, null, 0));}sendServiceArgsLocked(r, execInFg, true);if (r.delayed) {if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "REM FR DELAY LIST (new proc): " + r);getServiceMapLocked(r.userId).mDelayedStartList.remove(r);r.delayed = false;}if (r.delayedStop) {// Oh and hey we've already been asked to stop!r.delayedStop = false;if (r.startRequested) {if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE,"Applying delayed stop (from start): " + r);stopServiceLocked(r);}}}

1、scheduleCreateService
利用ApplicationThread 向app进程发送IPC消息

ApplicationThread.scheduleCreateService()
ApplicationThread发送H.CREATE_SERVICE消息
ActivityThread.handleCreateService()
ActivityThread类

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);}

handleCreateService()中完成了以下操作:

利用LoadedApk创建了Service实例,
创建ContextImpl()实例
调用Service.attach()完成ContextImpl的绑定
调用Service.onCreate()方法

2、 sendServiceArgsLocked 启动Service的参数处理
ActiveServices.sendServiceArgsLocked 主要做的操作就是,通过ApplicationThread发送scheduleServiceArgs的IPC消息

    private final void sendServiceArgsLocked(ServiceRecord r, boolean execInFg,boolean oomAdjusted) throws TransactionTooLargeException {final int N = r.pendingStarts.size();if (N == 0) {return;}ArrayList<ServiceStartArgs> args = new ArrayList<>();while (r.pendingStarts.size() > 0) {ServiceRecord.StartItem si = r.pendingStarts.remove(0);if (DEBUG_SERVICE) {Slog.v(TAG_SERVICE, "Sending arguments to: "+ r + " " + r.intent + " args=" + si.intent);}if (si.intent == null && N > 1) {// If somehow we got a dummy null intent in the middle,// then skip it.  DO NOT skip a null intent when it is// the only one in the list -- this is to support the// onStartCommand(null) case.continue;}si.deliveredTime = SystemClock.uptimeMillis();r.deliveredStarts.add(si);si.deliveryCount++;if (si.neededGrants != null) {mAm.mUgmInternal.grantUriPermissionUncheckedFromIntent(si.neededGrants,si.getUriPermissionsLocked());}mAm.grantImplicitAccess(r.userId, si.intent, si.callingId,UserHandle.getAppId(r.appInfo.uid));bumpServiceExecutingLocked(r, execInFg, "start");if (!oomAdjusted) {oomAdjusted = true;mAm.updateOomAdjLocked(r.app, true, OomAdjuster.OOM_ADJ_REASON_START_SERVICE);}if (r.fgRequired && !r.fgWaiting) {if (!r.isForeground) {if (DEBUG_BACKGROUND_CHECK) {Slog.i(TAG, "Launched service must call startForeground() within timeout: " + r);}scheduleServiceForegroundTransitionTimeoutLocked(r);} else {if (DEBUG_BACKGROUND_CHECK) {Slog.i(TAG, "Service already foreground; no new timeout: " + r);}r.fgRequired = false;}}int flags = 0;if (si.deliveryCount > 1) {flags |= Service.START_FLAG_RETRY;}if (si.doneExecutingCount > 0) {flags |= Service.START_FLAG_REDELIVERY;}args.add(new ServiceStartArgs(si.taskRemoved, si.id, flags, si.intent));}ParceledListSlice<ServiceStartArgs> slice = new ParceledListSlice<>(args);slice.setInlineCountLimit(4);Exception caughtException = null;try {//ActiveServices.sendServiceArgsLocked 主要做的操作就是,通过ApplicationThread发送scheduleServiceArgs的IPC消息r.app.thread.scheduleServiceArgs(r, slice);} catch (TransactionTooLargeException e) {if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Transaction too large for " + args.size()+ " args, first: " + args.get(0).args);Slog.w(TAG, "Failed delivering service starts", e);caughtException = e;} catch (RemoteException e) {// Remote process gone...  we'll let the normal cleanup take care of this.if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "Crashed while sending args: " + r);Slog.w(TAG, "Failed delivering service starts", e);caughtException = e;} catch (Exception e) {Slog.w(TAG, "Unexpected exception", e);caughtException = e;}if (caughtException != null) {// Keep nesting count correctfinal boolean inDestroying = mDestroyingServices.contains(r);for (int i = 0; i < args.size(); i++) {serviceDoneExecutingLocked(r, inDestroying, inDestroying);}if (caughtException instanceof TransactionTooLargeException) {throw (TransactionTooLargeException)caughtException;}}}

ApplicationThread.scheduleServiceArgs()

 public final void scheduleServiceArgs(IBinder token, ParceledListSlice args) {List<ServiceStartArgs> list = args.getList();for (int i = 0; i < list.size(); i++) {ServiceStartArgs ssa = list.get(i);ServiceArgsData s = new ServiceArgsData();s.token = token;s.taskRemoved = ssa.taskRemoved;s.startId = ssa.startId;s.flags = ssa.flags;s.args = ssa.args;sendMessage(H.SERVICE_ARGS, s);}}

发送H.SERVICE_ARGS 消息
ActivityThread.handleServiceArgs(),调用Service.onStartCommand()

private void handleServiceArgs(ServiceArgsData data) {Service s = mServices.get(data.token);if (!data.taskRemoved) {res = s.onStartCommand(data.args, data.flags, data.startId);} else {s.onTaskRemoved(data.args);res = Service.START_TASK_REMOVED_COMPLETE;}}

service启动流程相关推荐

  1. 深入分析Android 9.0源代码——Service启动流程(startService方式)

    引言 点击此处查看<深入分析Android 9.0源代码>系列的组织结构和相关说明. 1 应用进程发起启动请求 本章的调用流程如下图所示: (Context)ContextWrapperC ...

  2. (七十)Android O Service启动流程梳理——bindService

    前言:最近在处理anr问题的时候迫切需要搞清楚service的启动流程,抽时间梳理一下. 1.service启动简述 service启动分三种,比较简单的就是startService,Android ...

  3. Service启动流程总结-bind和unbind

    文章目录 回顾 概述 基本使用 源码探究 bind过程 Caller发起bind IServiceConnection说明 AMS处理bind请求 Service处理bind请求 AMS发布Servi ...

  4. Android service启动流程分析.

    文章仅仅用于个人的学习记录,基本上内容都是网上各个大神的杰作,此处摘录过来以自己的理解学习方式记录一下. 参考链接: https://my.oschina.net/youranhongcha/blog ...

  5. 以Binder视角来看Service启动

    一. 概述 在前面的文章startService流程分析,从系统framework层详细介绍Service启动流程,见下图: Service启动过程中,首先在发起方进程调用startService,经 ...

  6. 【Android】Service启动、生命周期

    service启动流程 startService方式 每个App进程中至少会有两个binder线程 ApplicationThread(简称AT)和ActivityManagerProxy(简称AMP ...

  7. service启动activity_「 Android 10 四大组件 」系列—Service 的 quot; 启动流程 quot;

    作者:DeepCoder 核心源码 关键类路径 Service 的启动过程相对 Activity 的启动过程来说简单了很多,我们都知道怎么去创建和启动一个 Service, 那么你有没有从源码角度研究 ...

  8. andriod studio 启动service失败_惊呆了!女儿拿着小天才电话手表,问我Android启动流程!...

    阅读本文大概需要15分钟 作者:巨人大哥出处:https://blog.csdn.net/CHAMPION8888/article/details/108490500 首先,new一个女儿, var ...

  9. 解析并符号 读取dll_Spring IOC容器之XmlBeanFactory启动流程分析和源码解析

    一. 前言 Spring容器主要分为两类BeanFactory和ApplicationContext,后者是基于前者的功能扩展,也就是一个基础容器和一个高级容器的区别.本篇就以BeanFactory基 ...

最新文章

  1. cloudstack 4.0 XenServer 日常简单故障处理
  2. C C++编程产生指定范围内的随机数
  3. Android View体系(三)属性动画
  4. string s.substr()的用法
  5. app上传头像处理Java_java后台加安卓端实现头像上传功能
  6. Java 8th 函数式编程:lambda 表达式
  7. bios设置开机双系统选择_打破专家的断言,突破微软和英特尔的封锁,惠普电脑玩转双系统...
  8. 养成一个习惯有多难?不如先从一个小目标开始
  9. 编程基础(三)——体系结构之三
  10. 为什么安监控需要公网ip_为什么这些重要场所需要安装电气火灾监控系统
  11. 金庸的武侠世界和SAP的江湖
  12. 百度地图-根据地址获取经纬度
  13. 全球高效能人士给青年的50个忠告(上) --转载
  14. 微软苏州研发人员将达4500人!第二幢楼今天开建!
  15. server 2016备份还原
  16. 信号完整性(SI)电源完整性(PI)学习笔记(二十一)差分对与差分阻抗(一)
  17. 微信公众号之微信登录
  18. 用python绘制圆中圆
  19. php 蓝奏网盘上传文件,教给大家一个蓝奏云高级玩法,如何把文件一键秒上传到蓝奏云网盘...
  20. 生成了一个严重警告并将其发送到远程终结点。这会导致连接终止。TLS 协议所定义的严重错误代码是...

热门文章

  1. shell 字符串处理汇总(查找,替换等等)
  2. 各种SD卡参数及接口
  3. 管理类联考——写作——素材篇——论说文——写作素材03——志篇:逆境·考验04——志篇:初心
  4. js layui跳转页面_layui lay-href不能成功跳转页面
  5. BSC链上项目再遭黑客攻击,“黑色5月”阴云持续?
  6. 互联金融风控-多头借贷
  7. 80年来最强龙卷风袭击古巴首都 致4死195伤
  8. python土味情话_python教你谈恋爱-之-土味情话5000条
  9. nodejs的前端项目搭建以及登陆接口开发
  10. 关于R4i的SDHC 3DS任天堂游戏的一些信息