Service 是一个 Android 组件,用于执行没有 UI 的操作 — 通常是后台中长时间运行的工作。有不同的类型(foreground、background、bound),每种都适合不同的需求,尽管现代 Android 在许多情况下倾向于使用 WorkManager。
什么是 Service
A SERVICE runs operations WITHOUT a UI (not tied to a screen):
→ for work that should continue regardless of UI (or independent of any activity)
→ declared in the manifest; runs in the app's process (main thread by default — must
move heavy work off it!)
Service 的类型
FOREGROUND SERVICE → user-aware, ongoing work with a PERSISTENT NOTIFICATION:
→ music playback, navigation, active location tracking, ongoing downloads
→ higher priority (less likely to be killed); the notification is required
BACKGROUND SERVICE → work without user awareness:
→ heavily RESTRICTED on modern Android (battery) → often use WorkManager instead
BOUND SERVICE → other components BIND to it for a client-server interface (interact with
it, call methods); lives while bound
