【问题标题】:How to make a daemon or service that can run forever and never exit in Android? [duplicate]如何制作一个可以永远运行并且永远不会在 Android 中退出的守护进程或服务? [复制]
【发布时间】:2013-08-25 22:29:59
【问题描述】:

如果设备资源不足,Android中的Service将被杀死。如何使守护进程或服务永远存在并且永不退出?

【问题讨论】:

    标签: android service daemon


    【解决方案1】:

    使用Service.startForeground(int id, Notification notification):http://developer.android.com/reference/android/app/Service.html#startForeground(int,android.app.Notification)

    会告诉系统该服务是用户需要的,不应该被杀死。 API 会强制您在此期间显示通知图标。

    但是,系统可以在极端情况下杀死它想要的任何东西以回收内存。

    【讨论】:

    • 如果我使用 startForeground,这会被第三方线程杀手应用杀死吗?
    【解决方案2】:

    启动的服务可以使用 startForeground(int, Notification) API 将服务置于前台状态,系统认为它是用户主动意识到的,因此不是内存不足时杀死的候选人。 (理论上,在当前前台应用程序的极端内存压力下,服务仍然可能被杀死,但实际上这应该不是问题。)

    【讨论】:

    • 如果我使用 startForeground,这会被第三方线程杀手应用杀死吗?
    • 是的。你无法阻止,你的服务在一个可以被杀死的普通 Linux 进程中运行......
    • 我不认为第三方线程杀手应用程序可以杀死它,因为可能反过来通过系统仅通过android沙盒机制来完成它。不过,我不是 100% 确定的。
    猜你喜欢
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 2021-08-05
    • 2023-03-17
    • 1970-01-01
    • 2015-02-06
    • 1970-01-01
    • 2011-03-12
    相关资源
    最近更新 更多