开发者

Android thread / service question

I am working on an idea for an app and want to really understand what role threads and services will play in my application. In a very simple app, I want a list view to be updated once a week from some sort of database. Ballpark 100 items. So from what I think i understand so far, I will have an activity which will have a UI consisting of a list view. I will then have a service. In this service I will have a thread, and this thread will be in charge of downl开发者_开发知识库oading. The service is there simply to make sure the download can continue if the app is paused or stopped. Then I can use a message/runnable/asyncTask to send this data over to my activity/UI from my thread. Am I right in thinking this?


I think you missed a BroasdCastReceiver:

I want a list view to be updated once a week

For this task you'll need to install an Alarm. Alarms usually start Broadcast receivers, to be sure that phone isn't sleeping. This BroadcastReceiver will have to start your service. I know it sounds complicated, but its quite ok to implement :)

Here are my comments for the remaining questions:

I will have an activity which will have a UI consisting of a list view

Correct

I will then have a service. In this service I will have a thread, and this thread will be in charge of downloading.

Thats also correct.

Then I can use a message/runnable/asyncTask to send this data over to my activity/UI from my thread.

That will maybe fail, because your service doesn't know if the main activity is running. Whats about writing the lists data into a shared file, both service and activity can access?


If your app is really as simple as you described, then you can do it without any services or even threading.

  1. You'll need an Activity with a ListView to display your content; This is a must
  2. You'll need to make an request to the internet and get your data. If you implement this part without another thread, your UI will be blocked and may cause Android OS to terminate your app but not if you can return your content within 5 seconds. So in this case, threading became optional.

If you do decide to use another thread to load data, you don't need a service to host the thread. Since you're only checking your remote service once per week, you'll only need to poll the remote service whenever your Activity is launched.

I think using a service to load data makes the app more complicated than it needs to be.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜