• unbindService流程-Android12


    unbindService流程-Android12

    service_lifecycle

    1. 服务生命周期(从创建到销毁)可遵循以下任一路径

    • 启动服务
        该服务在其他组件调用 startService() 时创建,然后无限期运行,且必须通过调用 stopSelf() 来自行停止运行。此外,其他组件也可通过调用 stopService() 来停止此服务。服务停止后,系统会将其销毁。

    • 绑定服务
        该服务在其他组件(客户端)调用 bindService() 时创建。然后,客户端通过 IBinder 接口与服务进行通信。客户端可通过调用 unbindService() 关闭连接。多个客户端可以绑定到相同服务,而且当所有绑定全部取消后,系统即会销毁该服务。(服务不必自行停止运行。)

    2. Service.java对应代码和unbindService流程时序图

    • removeConnectionLocked()移除绑定ServiceConnection
    • scheduleUnbindService()处理通知onUnbind()
    • bringDownServiceIfNeededLocked(s, true, hasAutoCreate, enqueueOomAdj);isServiceNeededLocked()判断是否还有服务绑定hasAutoCreate,若有绑定不会bringDownServiceLocked()(即handleStopService());而stopServiceLocked()传参都是false:bringDownServiceIfNeededLocked(service, false, false, enqueueOomAdj)
    • handleStopService()通知到onDestroy()
      在这里插入图片描述
    public class ExampleService extends Service {
        int startMode;       // indicates how to behave if the service is killed
        IBinder binder;      // interface for clients that bind
        boolean allowRebind; // indicates whether onRebind should be used
    
        @Override
        public void onCreate() {
            // The service is being created
        }
        @Override
        public int onStartCommand(Intent intent, int flags, int startId) {
            // The service is starting, due to a call to startService()
            return mStartMode;
        }
        @Override
        public IBinder onBind(Intent intent) {
            // A client is binding to the service with bindService()
            return mBinder;
        }
        @Override
        public boolean onUnbind(Intent intent) {
            // All clients have unbound with unbindService()
            return mAllowRebind;
        }
        @Override
        public void onRebind(Intent intent) {
            // A client is binding to the service with bindService(),
            // after onUnbind() has already been called
        }
        @Override
        public void onDestroy() {
            // The service is no longer used and is being destroyed
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34

    3. 先startService再bindService情况

    上面查看stopServiceLocked()传参虽然都是false,但是还是会检查hasConn = r.hasAutoCreateConnections()是否有绑定ServiceConnection

    还是会检查ServiceConnection

    这两条路径并非完全独立。用户已经使用 startService() 启动的服务,用户又通过调用 bindService() 绑定到服务。此类情况下,在所有客户端取消绑定之前,stopService() 或 stopSelf() 实际不会停止服务。

  • 相关阅读:
    包含min函数的栈、栈的压入弹出序列、从上往下打印二叉树、二叉搜索树的后序遍历序列
    曝 iPhone 设计主管明年 2 月离职,将联手 OpenAI 开发一款新 AI 硬件?
    Python启动Windows cmd(.bat)命令行并kill进程
    有人开源全凭“为爱发电”,有人却用开源“搞到了钱”
    SQL每日一练(牛客新题库)——第1天: 基础查询
    像你这么优秀的测试工程师,为什么连面试都过不了?
    抽象工厂模式
    golang--channal与select
    linux网络编程-socket-函数及TCP通信实现
    Python老手也会犯的20个新手级错误
  • 原文地址:https://blog.csdn.net/qq_23452385/article/details/125893321