开发者

How does extends AsyncTask<Void, Void, Long> work

开发者 https://www.devze.com 2023-02-26 03:32 出处:网络
I would开发者_如何学Go like to know how it works. I have read the documentation a couple of times but don’t get the idea.

I would开发者_如何学Go like to know how it works. I have read the documentation a couple of times but don’t get the idea.

I noticed that in some cases onPostExecute does nothing.

Can anyone please give me a little explanation?


AsyncTask is a class derived from Thread and provides you a simple and proper way of doing some things in the background with the ability of notifying the UI Thread.

In order to use it you should create a class, which extends it and define the type parameters. They are Params, Progress and Result. Read about them more here.

onPostExecute() is a method which is called when doInBackground() finished it's execution, but onPostExecute() is run on the UI Thread. So, you can notify the UI about the work has been done.

You can see an example of AsyncTask usage here.


The <Void, Void, Long> part is what in Java is called Generics. It is used in those classes where the original coder wants it to be used no matter what the types the "end user" will choose.

Typically you use onPre/PostExecute() when you want to start/stop a progress dialog.

If you want to update the UI during the progress without a progress dialog you can do things in onProgressUpdate() (i.e.: you're loading images from web and displaying them as they get loaded)

If you have a dialog with progress bar you'll have to use all of them to update the progress bar.


This post explains AsyncTask concept nicely with diagram !


{onPostExecute()} is used when you want to do something after completing your background task {(doInBackground())}.

For example,

start Progress Bar @ {onPreExecute()}
running Progress Bar @ {doInBackground()}
stop Progress Bar @ {onPostExecute()}
0

精彩评论

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

关注公众号