第一章:Service核心概念与生命周期
1.1 Service的定义与作用
Service是Android四大组件之一,用于在后台执行长时间运行的操作。其核心特性包括:
- 无用户界面:独立于UI线程运行
- 跨进程通信:通过Binder机制实现进程间通信(IPC)
- 优先级保留:系统在资源紧张时优先保留Service进程
生命周期状态转换模型如下:
stateDiagram [*] --> Created Created --> Started : startService() Started --> Running : onStartCommand() Running --> Stopped : stopSelf()/stopService() Stopped --> [*] Created --> Bound : bindService() Bound --> Running : onBind() Running --> Unbound : unbindService() Unbound --> [*]