android - 关于服务中的 START_STICKY

标签 android android-intent android-service

关于使用START_STICKY在 Android 服务中。

官方文件说:

if this service's process is killed while it is started (after returning from onStartCommand(Intent, int, int)), then leave it in the started state but don't retain this delivered intent. Later the system will try to re-create the service. Because it is in the started state, it will guarantee to call onStartCommand(Intent, int, int) after creating the new service instance;

这是可以理解的,因此当一个已启动的服务的进程因内存不足而被终止时,操作系统稍后会自动尝试再次启动该服务。

我的问题是,如果我的代码启动服务 startService(...),然后停止它(例如,我的应用程序的 UI 上有一个“停止”按钮,当按下它时,我调用stopService(...)),系统稍后会再次启动该服务吗?或者服务实例是否会从内存中完全清除而不再由系统创建? (此场景没有服务绑定(bind))

最佳答案

START_STICKY- 告诉系统在内存不足时恢复后,当有足够的内存可用时,创建服务的新副本。在这里您将丢失之前可能计算过的结果。

您的查询的答案是,只有在使用 START_STICKY 时在任何情况下被操作系统终止的服务才会重新创建。如果我们终止服务,为什么 android 需要再次重现它。如果您使用 stopSelf(),即使服务是粘性的,也不会重新创建。

关于android - 关于服务中的 START_STICKY,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32962593/

相关文章:

android - 混合列表 Activity

android - 在 Android 应用中提交带有 POST 数据的表单

android - 如何从android中的assets中获取pdf文件?

android - 如何监控 Android 设备中应用程序的后台服务?

java - 通过服务选择的插件架构

android - 如何更改 ActionBarSherlock 的选项卡文本颜色?

Android keystore UserNotAuthenticatedException 无限循环

android - 如何在提供商更改时重新注册 android 地理围栏?

android - 不要从浏览器打开自定义链接

android - 根据 Activity 的广播接收器更新 fragment ui