gpt4 book ai didi

android - 防止 Android 关闭我的服务/线程

转载 作者:行者123 更新时间:2023-11-30 04:06:18 25 4
gpt4 key购买 nike

我有一个应用程序在后台使用服务来执行一些上传/下载操作。这些操作可能需要一些时间,具体取决于用户的选择(文件的数量和大小)。

虽然在我的设备 (nexus s) 和测试模拟器中,一切都按计划工作,但我收到用户的报告,说在选择上传/下载大数据时,操作有时会卡住。

该操作是使用在后台服务中运行的线程完成的。我的猜测是操作系统有时会终止内存不足的设备上的线程和/或服务。

在服务 onStartCommand() 函数中,我启动了将执行操作的线程(要处理的数据在 Intent 内),然后我返回 START_NOT_STICKY。在我使用 startService(i) 的 Activity 中,没有完成绑定(bind)。

如何防止这种情况发生?如何确保系统不会终止我的服务或线程?

最佳答案

startForeground()的使用可能会帮助您实现目标。

来自文档:

A started service can use the startForeground(int, Notification) API to put the service in a foreground state, where the system considers it to be something the user is actively aware of and thus not a candidate for killing when low on memory. (It is still theoretically possible for the service to be killed under extreme memory pressure from the current foreground application, but in practice this should not be a concern.)

关于android - 防止 Android 关闭我的服务/线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11600459/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com