美文网首页
Android app是如何杀掉的

Android app是如何杀掉的

作者: wbo4958 | 来源:发表于2018-04-26 10:44 被阅读113次

    1. adb shell kill -9 pid_of_app

    AMS定义了AppDeathRecipient

    APP 在 attachApplication -> attachApplicationLocked
    AMS里会注册 App 进程的 BinderDeath通知

    AppDeathRecipient adr = new AppDeathRecipient(app, pid, thread);
    thread.asBinder().linkToDeath(adr, 0);
    

    当App进程被杀了后
    binder里的 binder_fops就会调用

    binder_release -> binder_deferred_func -> binder_deferred_release
    
    -> BINDER_WORK_DEAD_BINDER(binder_thread_read) ->  BR_DEAD_BINDER(binder_thread_read) -> BR_DEAD_BINDER(IPCThreadState::executeCommand)
    -> (BpBinder->sendObituary) -> (BpBinder->reportOneDeath) -> (JavaDeathRecipient->binderDied)
    -> BinderProxy.sendDeathNotice 
    -> ActivityManagerService.binderDied ->  appDiedLocked -> handleAppDiedLocked 清理相关资源 resumeFocusedStackTopActivityLocked
    

    注意,这种方式kill掉的进程不会调用 onDestroy

    link to death

    https://www.cnblogs.com/samchen2009/p/3316001.html

    2. 通过Recents里removeTasks杀掉进程

    AMS.removeTask -> ASS.removeTaskByIdLocked ->
    TaskRecord.removeTaskActivitiesLocked -> 这里面会去finish掉当前Activity, 也就是会调用onDestroy
    ASS.cleanUpRemovedTaskLocked -> ProcessRecord.kill -> 
    Process.killProcessQuiet -> sendSignalQuiet -> kill -9 pid (类似于kill -9)
    Process.killProcessGroup -> killProcessGroup
    

    后面的过程的1一样,通过binder death去通知AMS

    相关文章

      网友评论

          本文标题:Android app是如何杀掉的

          本文链接:https://www.haomeiwen.com/subject/bcqclftx.html