首页 > 其他分享 >Android 11 -- app 服务保活

Android 11 -- app 服务保活

时间:2024-03-24 17:46:16浏览次数:25  
标签:11 false uid service -- app stop services stopping

app服务保活 (android 11 源码)

frameworks/base/services/core/java/com/android/server/am/ActiveServices.java

stop后台service的逻辑:

AMS的doStopUidLocked方法会先stop 后台service,再去更新uid并分发uid的change
stopInBackgroundLocked回调后,service不一定会真被stop

void stopInBackgroundLocked(int uid) {
        // Stop all services associated with this uid due to it going to the background
        // stopped state.
        // 获取该uid下的所有service
        ServiceMap services = mServiceMap.get(UserHandle.getUserId(uid));
        ArrayList<ServiceRecord> stopping = null;
        if (services != null) {
            for (int i = services.mServicesByInstanceName.size() - 1; i >= 0; i--) {
                ServiceRecord service = services.mServicesByInstanceName.valueAt(i);
                 // start方式启动的service
                if (service.appInfo.uid == uid && service.startRequested) {
                    if (mAm.getAppStartModeLocked(service.appInfo.uid, service.packageName,
                            service.appInfo.targetSdkVersion, -1, false, false, false)
                            != ActivityManager.APP_START_MODE_NORMAL) {
                        if (stopping == null) {
                            stopping = new ArrayList<>();
                        }
                        String compName = service.shortInstanceName;

                        //add  包名过滤防止它被杀
                        if(compName.contains("com.google.wangyiyun"))
                            continue;
                        //end
                        ......
                        
                        // 將当前service添加进stoppping列表
                        stopping.add(service);

                        // If the app is under bg restrictions, also make sure that
                        // any notification is dismissed
                        if (appRestrictedAnyInBackground(
                                service.appInfo.uid, service.packageName)) {
                            cancelForegroundNotificationLocked(service);
                        }
                    }
                }
            }
            if (stopping != null) {
                for (int i=stopping.size()-1; i>=0; i--) {
                    ServiceRecord service = stopping.get(i);
                    service.delayed = false;
                    services.ensureNotStartingBackgroundLocked(service);
                    // stop service
                    stopServiceLocked(service);
                }
            }
        }
    }
    

//这个函数用停止app的服务
private void stopServiceLocked(ServiceRecord service) {
        if (service.delayed) {
            // If service isn't actually running, but is being held in the
            // delayed list, then we need to keep it started but note that it
            // should be stopped once no longer delayed.
            if (DEBUG_DELAYED_STARTS) Slog.v(TAG_SERVICE, "Delaying stop of pending: " + service);
            service.delayedStop = true;
            return;
        }
        StatsLog.write(StatsLog.SERVICE_STATE_CHANGED, service.appInfo.uid,
                service.name.getPackageName(), service.name.getClassName(),
                StatsLog.SERVICE_STATE_CHANGED__STATE__STOP);
        synchronized (service.stats.getBatteryStats()) {
            service.stats.stopRunningLocked();
        }
        service.startRequested = false;
        if (service.tracker != null) {
            service.tracker.setStarted(false, mAm.mProcessStats.getMemFactorLocked(),
                    SystemClock.uptimeMillis());
        }
        service.callStart = false;

        bringDownServiceIfNeededLocked(service, false, false);
    }
    

bringDownServiceIfNeededLocked(...)
private final void bringDownServiceIfNeededLocked(ServiceRecord r, boolean knowConn,
            boolean hasConn) {

        if (isServiceNeededLocked(r, knowConn, hasConn)) {
            return;
        }
        // 如果有新拉起service的需求,本次不会stop该service
        if (mPendingServices.contains(r)) {
            return;
        }
        bringDownServiceLocked(r);
    }

    

(android-12)app进入后台,关掉杀死serivce
参考一

标签:11,false,uid,service,--,app,stop,services,stopping
From: https://www.cnblogs.com/kato-T/p/18092710

相关文章

  • pedagogic metalanguage
    MyfavoriteEnglishvideoclipisfromthemovieFlyingringtravel.Therearetwomaincharactersinthevideoclip:CarlFredricksenandRussell.Underthecontinuousdevelopmentofthecity.CarlFredricksenmadeashockingmovetorealizeadreamheshar......
  • (Service)服务启动流程分析(Android 10.0)
    (Service)服务启动流程分析,AMS内保活服务(Android10.0)frameworks/base/services/core/java/com/android/server/am/ActivityManagerService.javaframeworks/base/services/core/java/com/android/server/am/ActiveServices.javaframeworks/base/core/java/android/app/Contex......
  • function函数和箭头函数的区别,用法和功能方面的区别
    在JavaScript中,普通函数(functionfunctions)和箭头函数(arrowfunctions)有一些区别,包括用法和功能方面的不同之处:语法:普通函数使用关键字 function 来声明函数,例如:functionmyFunction(){...}。箭头函数使用箭头 => 来声明函数,例如:constmyFunction=()=>{...}......
  • 创建子域
    一:在dns3上安装AD域服务配置dns3IP首选dns服务器填dns1的IP备用dns服务器填dns2的IP勾选AD域服务一直点下一步直到安装二:将dns3升为子域的域控制器点击感叹号选择提升选择添加到现有林,输入域名在更改中输入dns1的域管理员账号密码输入虚拟机的密码一直点击下一步......
  • Grafana源码启动
    Grafana源码启动因为项目用到Grafana,但是用iframe嵌入前端。想研究一下源码看有没有可能直接用组件嵌入,然后用API调用Grafana的服务。GrafanabackendGrafana的后端服务是用Go写的,新语言,有待学习。Grafanafrontend前端使用React、Angular编写的Typescript项目。框架没学习过......
  • linux curl p12请求
    ##查看curl版本curl-V##查看curl指令curl--help#--cert证书#--cert-typeTYPECertificatefiletype(DER/PEM/ENG)(SSL)由此可看出linux下curl暂不支持p12证书由p12生成pem证书##生成时如果p12证书有密码,需要输入密码##生成key文件命令opensslpkcs12......
  • 目录
    日常笔记:24/03/21:VPCF1945。24/03/20:贪心。24/03/14:VPCF1941。24/03/13:序列DP。24/03/12:区间DP。AtCoder比赛记录:AtCoderBeginnerContest346:手速与脑力的巅峰,最刺激的一集。AtCoderBeginnerContest341AtCoderBeginnerContest340AtCoderBeginnerConte......
  • 2024-03-24
    \({\color{Orange}\star}\)2024-03-24\({\color{Orange}\star}\)完全平方数题意就是求出第\(k\)个不是完全平方数的倍数的数随着数\(n\)的增加\([1,n]\)的满足条件的数的个数是单调不降的可以二分\(n\)的值,然后算出\([1,n]\)中满足条件的数的个数,根据它与\(k\)......
  • [暴力题解系列]2023年蓝桥杯-整数删除(30分)
    这题暴力最多30分,但是30分也是分,做暴力的人不能贪心,拿到分就是赚了。​ 这题核心烦人点在于他数据分层断崖,就只有前3个点能做到稳过。用的思路就是链表,但不是用指针存的,而是用数组下标为标记存的,只是我觉得因为这样好写一些。链表方便修改左右连接位置,所以越到后面就越能省下查询......
  • reflective journal
    Whatlhavelearntlhavelearntalotforthefirsttwoworkshops.Notonlylearnedsomevocabularyandwritingskills,butalsoimprovedsomeknowledgeaboutvideoproduction.Firstly,IunderstandwhatissemioticchoiceandIcameclosertounderstand......