gpt4 book ai didi

java - Android 服务持久化

转载 作者:行者123 更新时间:2023-12-02 00:49:16 25 4
gpt4 key购买 nike

在我的应用程序中,我实现了一个远程服务,该服务每 20 分钟与网络服务器交换一次数据,并修改扩展应用程序的 MyApplication 类中某些对象的值。

12 或 15 小时后,android 终止 MyApplication 类和/或我的服务。我尝试实现

android:alwaysRetainTaskState="true"

没有结果。有人可以向我解释如何使服务或应用程序在android中持久存在?我看到更多应用程序在后台停留数天而没有出现问题,但我不知道该怎么做。

我知道,如果每个应用程序需要一些可用内存,android 可能会杀死它,但我的系统有 180mb 的可用内存,并且没有应用程序在运行,因为我用它来测试我的程序。

最佳答案

Some could explain to me how to make a service or an application persistent in android?

你不知道。您编写的应用程序不需要始终位于内存中。

In my application I implemented a remote service that exchange data with a webserver each 20 minutes and modify the value of some object in MyApplication Class that extends Application.

请使用 AlarmManagerIntentService,这样您的服务就不需要位于内存中,除非它正在执行有意义的工作。这也意味着当您在内存中时,Android 不太可能杀死您的服务,并且用户也不太可能因为认为您在浪费内存而杀死您的服务。

关于java - Android 服务持久化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3899924/

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