开发者

Android : background thread [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

Team,

I need to run a background thread in my application. Could you please share the best practices where to initiate the thread so that keeps running irrespective of the Activity is being shown and thing开发者_StackOverflow中文版s to consider. The purpose of this background thread is to fire transactions from the Simulator to the server and get the response back from the host.

thanks, Ramesh


I solved this by having a custom application object hold a reference to the background thread (an AsyncTask in my case). The app object stays alive as long as your process runs. In other words, it won't get killed if your activity dies, for example if your change the phone orientation. An activity would set the current activity on the AsyncTask on creation, and unset the activity on the AsyncTask when the activity goes away. The AsyncTask would clear its own reference from the app object once it finished. I blogged about this here.


Sounds like you want a Service. Check out the Services section on the Application Fundamentals dev guide page.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜