Android前台服务优先级高难被杀死,返回START_STICKY是否有意义?
START_STICKY Make Sense for Foreground Services? Great question—let’s cut through the assumptions here. While foreground services do have extremely high priority and rarely get killed by the system, they aren’t completely invincible. Returning START_STICKY still serves practical purposes in specific scenarios:
Foreground services can still be killed in extreme low-memory situations
Even with foreground priority, if the system is under crippling memory pressure (think multiple active foreground apps + critical system processes), it may terminate your service. In this case,START_STICKYtells the system to attempt restarting your service once memory becomes available. Without this return value, the service would stay dead unless your app explicitly restarts it.Restarted services need to re-establish foreground status
When the system restarts your service viaSTART_STICKY, it starts in a background state by default. YouronStartCommandimplementation can check this, re-callstartForeground()with your persistent notification, and restore the service’s high-priority status. This lets you seamlessly resume critical functionality (like music playback, location tracking) without user intervention.Preserves basic restart behavior for non-user-initiated kills
If your service is terminated by system updates, crash recovery, or other non-user actions,START_STICKYensures the system tries to bring it back up. This is crucial for services that need to run continuously (e.g., a fitness tracker logging activity) even after unexpected system-level interruptions.
Important caveat: START_STICKY won’t help if the user manually kills your service (via Settings > Apps > Force Stop, or recent apps swipes on some devices). In those cases, the system respects the user’s choice and won’t restart the service automatically.
内容的提问来源于stack exchange,提问作者Florian Walther

