简体   繁体   English

AsyncTask中的httpClient是否需要wakeLock?

[英]Does httpClient in AsyncTask need wakeLock?

I am executing httpClient in AsyncTask but before this another AsyncTask is used to generate some data, which can take long time. 我在AsyncTask中执行httpClient但在此之前另一个AsyncTask用于生成一些数据,这可能需要很长时间。 If device gets sleep before executing httpClient, what would happen? 如果设备在执行httpClient之前进入睡眠状态,会发生什么? Do I need wakeLock for httpClient requests? 我是否需要wakeLock来获取httpClient请求?

If the device go sleep, your activity onPause() is called. 如果设备进入休眠状态,则会调用onPause()活动。 If you don't cancel/terminate the background task in some fancy way, nothing happens to it. 如果您没有以某种奇特的方式取消/终止后台任务,则没有任何反应。 AsyncTask are run in a separate thread than the main application one, so nothing can stop them. AsyncTask在一个独立的线程中运行,而不是主应用程序,所以没有什么可以阻止它们。 Ever. 永远。

I didn't understand your design: what's the purpose of two separate AsyncTask s? 我不明白你的设计:两个独立的AsyncTask的目的是什么? If gathering data to fill the HTTP request consumes time, you should use a single AsyncTask and do everything inside doInBackground() , to ensure no race condition happens. 如果收集数据以填充HTTP请求会消耗时间,则应使用单个AsyncTask并执行doInBackground()内的所有doInBackground() ,以确保不会发生竞争条件。 Otherwise you'd have to chain tasks, or fire the second one in the first one, and this doesn't sound right. 否则你必须链接任务,或者在第一个任务中触发第二个任务,这听起来不对。

The HTTP client is just a socket library, is not a mythological beast :) HTTP客户端只是一个套接字库,不是神话中的野兽:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM