开发者

Standalone Worker Thread or IntentService?

I'm new to Android. I've been reading several posts and documentation about Threads/Services, and I still can't figure out which would be the most suitable solution for my app, so I was wondering if you could give me your advice.

My main UI is supposed to have "start" and "stop" buttons. "start" would create a Thread that starts writing info 开发者_开发知识库(i.e battery statistics, RSSI, or whatever) to a file. After pressing "start", I will be switching to other applications, so my UI will be on "Stopped" state (If I didn't misunderstand the life cycle). Eventually, I will come back to my app and I want to be able to stop the thread.

For a long-running task like downloading/upload a large file I read the most suitable way is to have an IntentService, so that even if the application is stopped (or destroyed if not enough memory) the services continues its task until finished. As in my app the service won't stop by itself (I want it to be stopped with a stopService command), is a service still the best way to proceed? or should I just use a worker thread as my UI is not likely to be killed under normal circumstances?

Would you recommend writing directly to a file, or using sqlite?


What you described is looks like Service. It works independently of activity, but can be stopped from it when necessary. Threads or like a better solution from android, AsyncTask, are used for not so long things, like downloading not a large file.

As for the second question, it depends. If you data is rather complicated, has relationships inside and should be queried in several ways, then sqlite is the right choice. Otherwise you can simply write data to the file.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜