ComponentName startServiceLocked(IApplicationThread caller, Intent service, String resolvedType,
328int callingPid, int callingUid, boolean fgRequired, String callingPackage, finalint userId)
329 throws TransactionTooLargeException {
330if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "startService: " + service
331 + " type=" + resolvedType + " args=" + service.getExtras());
332333finalboolean callerFg;
334if (caller != null) {
335final ProcessRecord callerApp = mAm.getRecordForAppLocked(caller);
336if (callerApp == null) {
337thrownew SecurityException(
338"Unable to find app for caller " + caller
339 + " (pid=" + callingPid
340 + ") when starting service " + service);
341 }
342 callerFg = callerApp.setSchedGroup != ProcessList.SCHED_GROUP_BACKGROUND;
343 } else {
344 callerFg = true;
345 }
346347 ServiceLookupResult res =
348 retrieveServiceLocked(service, resolvedType, callingPackage,
349 callingPid, callingUid, userId, true, callerFg, false);
350if (res == null) {
351returnnull;
352 }
353if (res.record == null) {
354returnnew ComponentName("!", res.permission != null355 ? res.permission : "private to package");
356 }
357358 ServiceRecord r = res.record;
359360if (!mAm.mUserController.exists(r.userId)) {
361 Slog.w(TAG, "Trying to start service with non-existent user! " + r.userId);
362returnnull;
363 }
364365// If this isn't a direct-to-foreground start, check our ability to kick off an366// arbitrary service367if (!r.startRequested && !fgRequired) {
368// Before going further -- if this app is not allowed to start services in the369// background, then at this point we aren't going to let it period.370finalint allowed = mAm.getAppStartModeLocked(r.appInfo.uid, r.packageName,
371 r.appInfo.targetSdkVersion, callingPid, false, false);
372if (allowed != ActivityManager.APP_START_MODE_NORMAL) {
373 Slog.w(TAG, "Background start not allowed: service "374 + service + " to " + r.name.flattenToShortString()
375 + " from pid=" + callingPid + " uid=" + callingUid
376 + " pkg=" + callingPackage);
377if (allowed == ActivityManager.APP_START_MODE_DELAYED) {
378// In this case we are silently disabling the app, to disrupt as379// little as possible existing apps.380returnnull;
381 }
382// This app knows it is in the new model where this operation is not383// allowed, so tell it what has happened.384 UidRecord uidRec = mAm.mActiveUids.get(r.appInfo.uid);
385returnnew ComponentName("?", "app is in background uid " + uidRec);
386 }
387 }
388389 NeededUriGrants neededGrants = mAm.checkGrantUriPermissionFromIntentLocked(
390 callingUid, r.packageName, service, service.getFlags(), null, r.userId);
391392// If permissions need a review before any of the app components can run,393// we do not start the service and launch a review activity if the calling app394// is in the foreground passing it a pending intent to start the service when395// review is completed.396if (mAm.mPermissionReviewRequired) {
397if (!requestStartTargetPermissionsReviewIfNeededLocked(r, callingPackage,
398 callingUid, service, callerFg, userId)) {
399returnnull;
400 }
401 }
402403if (unscheduleServiceRestartLocked(r, callingUid, false)) {
404if (DEBUG_SERVICE) Slog.v(TAG_SERVICE, "START SERVICE WHILE RESTART PENDING: " + r);
405 }
406 r.lastActivity = SystemClock.uptimeMillis();
407 r.startRequested = true;
408 r.delayedStop = false;
409 r.fgRequired = fgRequired;
410 r.pendingStarts.add(new ServiceRecord.StartItem(r, false, r.makeNextStartId(),
411 service, neededGrants, callingUid));
412413final ServiceMap smap = getServiceMapLocked(r.userId);
414boolean addToStarting = false;
415if (!callerFg && !fgRequired && r.app == null416 && mAm.mUserController.hasStartedUserState(r.userId)) {
417 ProcessRecord proc = mAm.getProcessRecordLocked(r.processName, r.appInfo.uid, false);
418if (proc == null || proc.curProcState > ActivityManager.PROCESS_STATE_RECEIVER) {
419// If this is not coming from a foreground caller, then we may want420// to delay the start if there are already other background services421// that are starting. This is to avoid process start spam when lots422// of applications are all handling things like connectivity broadcasts.423// We only do this for cached processes, because otherwise an application424// can have assumptions about calling startService() for a service to run425// in its own process, and for that process to not be killed before the426// service is started. This is especially the case for receivers, which427// may start a service in onReceive() to do some additional work and have428// initialized some global state as part of that.429if (DEBUG_DELAYED_SERVICE) Slog.v(TAG_SERVICE, "Potential start delay of "430 + r + " in " + proc);
431if (r.delayed) {
432// This service is already scheduled for a delayed start; just leave433// it still waiting.434if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Continuing to delay: " + r);
435return r.name;
436 }
437if (smap.mStartingBackground.size() >= mMaxStartingBackground) {
438// Something else is starting, delay!439 Slog.i(TAG_SERVICE, "Delaying start of: " + r);
440 smap.mDelayedStartList.add(r);
441 r.delayed = true;
442return r.name;
443 }
444if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Not delaying: " + r);
445 addToStarting = true;
446 } elseif (proc.curProcState >= ActivityManager.PROCESS_STATE_SERVICE) {
447// We slightly loosen when we will enqueue this new service as a background448// starting service we are waiting for, to also include processes that are449// currently running other services or receivers.450 addToStarting = true;
451if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE,
452"Not delaying, but counting as bg: " + r);
453 } elseif (DEBUG_DELAYED_STARTS) {
454 StringBuilder sb = new StringBuilder(128);
455 sb.append("Not potential delay (state=").append(proc.curProcState)
456 .append(' ').append(proc.adjType);
457 String reason = proc.makeAdjReason();
458if (reason != null) {
459 sb.append(' ');
460 sb.append(reason);
461 }
462 sb.append("): ");
463 sb.append(r.toString());
464 Slog.v(TAG_SERVICE, sb.toString());
465 }
466 } elseif (DEBUG_DELAYED_STARTS) {
467if (callerFg || fgRequired) {
468 Slog.v(TAG_SERVICE, "Not potential delay (callerFg=" + callerFg + " uid="469 + callingUid + " pid=" + callingPid + " fgRequired=" + fgRequired + "): " + r);
470 } elseif (r.app != null) {
471 Slog.v(TAG_SERVICE, "Not potential delay (cur app=" + r.app + "): " + r);
472 } else {
473 Slog.v(TAG_SERVICE,
474"Not potential delay (user " + r.userId + " not started): " + r);
475 }
476 }
477478 ComponentName cmp = startServiceInnerLocked(smap, service, r, callerFg, addToStarting);
479return cmp;
480 }
复制代码