Question

I love IntentService and try and use it whenever I have to write a Service.

However I have to perform a long task (~4 seconds) at least once, when the application starts. The task is to load a raw resource (a certificate) and use it for all web service calls. Currently I'm loading it each time a web service call is made, which is obviously not a real solution.

Because IntentService is stateless I can't keep the certificate around between requests. What are my options other than writing a "normal" Service and implementing the lifecycle carefully to check when the certificate should be (re)loaded.

Thanks.

Was it helpful?

Solution

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

Loading a raw resource should take milliseconds. Use Traceview to figure out why it is taking ~4 seconds.

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

Loading a raw resource every time seems like a perfectly viable solution. It's not like you have another choice.

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

That would be a really bad idea.

Users greatly dislike developers who keep services running when they shouldn't. Users attack apps like this with task killers and force-stops in Settings. Once that happens, on Android 3.1+, your app will never run again until the user starts up one of your activities.

Also, Android will terminate your everlasting service after a while, once again to protect users from poorly-written services.

Focus instead on optimizing the ~4 second operation.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top