开发者

How to slow down the doing background in asynctask when internet connection down

开发者 https://www.devze.com 2023-03-23 08:07 出处:网络
i use asynctask to do the GET request to server, and What i want to do is when 开发者_开发知识库the internet connection down ( no internet connection ) i want my AsynTask sleep abit then start send re

i use asynctask to do the GET request to server, and What i want to do is when 开发者_开发知识库the internet connection down ( no internet connection ) i want my AsynTask sleep abit then start send request again in the same asynctask , How can i do that

THx


A logical thing to do would be listen to connectivity change broadcast and cancel the task when no connection available and restart it when the connection gets available again. You could check this Q to get the BroadcastReceiver ready.


Documentation says:

**Threading rules**

There are a few threading rules that must be followed for this class to work properly:

The task instance must be created on the UI thread.

execute(Params...) must be invoked on the UI thread.

Do not call onPreExecute(), onPostExecute(Result), doInBackground(Params...), onProgressUpdate(Progress...) manually.

The task can be executed only once (an exception will be thrown if a second execution is attempted.)

Try using Handler, do the fetching HTTP stuff in Thread (you can make thread sleep for some amount of time using Thread.sleep(time); when you think you are ready to proceed call .sendMessage(message); and do the updates on the ui from the handler (handleMessage() or runOnUiThread() methods) if that is needed.

0

精彩评论

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

关注公众号