0

Right now I have a server running on a desktop. I want to be able to start up my app, hit a button to start collecting data from this server, and only stop once I hit to button again. The user should always be grabbing data from this server after hitting the button, even if the app isn't active. So far I've considered setting up a Service or using Threads by themself.

A Service sounds exactly like what I need, but I've been told it isn't meant to sustain a long network connection. I poked through the BluetoothChat sample application and it didn't use a Service. Would a Service be the right thing to use then, or should I implement it with threads like the sample application does? The only reason I need a long connection is to listen for any error reports from the server. The other network stuff only happens when the user is directly using the app. I will have to use threads anyways because a Service runs in the same thread as the activity that calls it, but I guess my main question is whether I should scrap the Service part. Right now I have a basic Service set up that can handle messages sent to it and it seems really easy to use. The documentation on it is just all over the place, there needs to be a section saying "If you want to do X, then this is suggested!"

4

1 回答 1

1

Service 在 UI 线程上运行,而 IntentService 在自己的线程中运行。

说“应用程序”不活动是模棱两可的。如果您正在运行服务,它始终处于活动状态。只要 IntentService 完成其 onHandleIntent() 方法中的工作,它就处于活动状态。应用程序的所有活动都可能不处于活动状态,这意味着应用程序处于后台。

除此之外,我需要更多地了解你想要做什么。一般来说,最好是循环收集数据,而不是试图保持连接不断打开。例如,当设备失去与 Internet 的连接时,您会怎么做?

于 2012-06-09T00:06:50.463 回答