Android IntentService 和状态

标签 android intentservice

我喜欢 IntentService 并在我必须编写服务时尝试使用它。

但是,当应用程序启动时,我必须至少执行一次长任务(~4 秒)。任务是加载原始资源(证书)并将其用于所有 Web 服务调用。目前,每次进行 Web 服务调用时我都会加载它,这显然不是真正的解决方案。

因为 IntentService 是无状态的,所以我无法在请求之间保留证书。除了编写“正常”服务并仔细实现生命周期以检查何时应(重新)加载证书外,我还有什么选择。

谢谢。

最佳答案

The task is to load a raw resource (a certificate) and use it for all web service calls.

加载原始资源需要几毫秒。使用 Traceview 找出为什么需要大约 4 秒。

Currently I'm loading it each time a web service call is made, which is obviously not a real solution.

每次加载原始资源似乎是一个完全可行的解决方案。并不是说您有其他选择。

What are my options other than writing a "normal" Service and implementing the lifecycle carefully to check when the certificate should be (re)loaded.

那将是一个非常糟糕的主意。

用户非常不喜欢在不应该运行的情况下保持服务运行的开发人员。用户在“设置”中使用任务 killer 和强制停止来攻击此类应用程序。一旦发生这种情况,在 Android 3.1+ 上,您的应用将永远不会再次运行,直到用户启动您的 Activity 之一。

此外,Android 将在一段时间后终止您的永久服务,再次保护用户免受编写不当的服务的侵害。

而不是专注于优化 ~4 秒的操作。

关于Android IntentService 和状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7992959/

相关文章:

android - 奇怪 - Android IntentService 杀死了我应用程序的其余部分,但继续自行运行

android - Robolectric 和 IntentServices

java - 如何保护 Java 程序免受黑客攻击

android - 适当的插件弹出

android - Tabris 示例

android - 用于将大型(2 GB)文件上传到 android 中的服务器的服务/IntentService

android - 我在后台的 IntentService 如何始终监听设备的震动?

android - Android Manifest 中如何声明 Intent 服务?

android - Android 上的 FFMPEG "no such file or directory"

java - 如何创建 Android 通知?