Android组件管理框架—后台服务Service之startService方式启动流程(Android P)

本文详细探讨了在Android P系统中,如何通过startService启动Service组件的过程。从ContextWrapper开始,经过ContextImpl、ActivityManagerService、ActiveServices到ServiceRecord的创建,再到应用程序进程的Service生命周期方法调用,包括onCreate和onStartCommand。文章通过图示和代码分析,清晰展示了Service启动的完整路径。
摘要由CSDN通过智能技术生成

一 前言

       Service组件是Android应用四大组件之一,主要用来处理与用户界面无关的逻辑。Service的启动过程与Activity的启动过程是类似的。和Activity一样,启动过程由ActivityManagerService来管理。我们下面分析的是启动的Service和启动者(可以是Activity、Service等)是在同一个进程,在此假设是在Activity启动的。

     也可以复习一下Android组件管理框架—视图容器Activity之启动流程(Android P)

 

二 图示调用流程

 

三 代码具体流程

 1 frameworks/base/core/java/android/content/ContextWrapper.java

@Override
    public ComponentName startService(Intent service) {
        return mBase.startService(service);
    }
}

这里mBase变量是ContextImpl类型,在创建activity时new 的一个ContextImpl对象,赋值给activity。

2 frameworks/base/core/java/android/app/ContextImpl.java

@Override
    public ComponentName startService(Intent service) {
        warnIfCallingFromSystemProcess();
        return startServiceCommon(service, false, mUser);
    }
}

接着看startServiceCommon。

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

       ActivityManager.getService().startService,直接返回的是ActivityManagerService的实例,这一块和startActivity的过程一样,具体就不展开说了,可以参考之前的startActivity的启动流程,Android组件管理框架—视图容器Activity之启动流程(Android P)

3 frameworks/base/core/java/android/app/ActivityManager.java

4 frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.java

调用到系统进程(System_server)来了。

@Override
public ComponentName startService(IApplicationThread caller, Intent service,
        String resolvedType, boolean requireForeground, String callingPackage, int userId)
        throws TransactionTooLargeException {
    enforceNotIsolatedCaller("startService");
    // Refuse possible leaked file descriptors
    if (service != null && service.hasFileDescriptors() == true) {
        throw new IllegalArgumentException("File descriptors passed in Intent");
    }

    if (callingPackage == null) {
        throw new IllegalArgumentException("callingPackage cannot be null");
    }

    if (DEBUG_SERVICE) Slog.v(TAG_SERVICE,
            "*** startService: " + service + " type=" + resolvedType + " fg=" + requireForeground);
    synchronized(this) {
        final int callingPid = Binder.getCallingPid();
        final int callingUid = Binder.getCallingUid();
        final long origId = Binder.clearCallingIdentity();
        ComponentName res;
        try {
            res = mServices.startServiceLocked(caller, service,
                    resolvedType, callingPid, callingUid,
                    requireForeground, callingPackage, userId);
        } finally {
            Binder.restoreCallingIdentity(origId);
        }
        return res;
    }
}

接着看mServices.startServiceLocked,mServices是ActiveService的实例。

5 frameworks/base/services/core/java/com/and

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值