You are on page 1of 12

AsynTask &Thread

Day-1
facebook.com/apex.
tgi
twitter.com/
ApextgiNoida
pinterest.com/
apextgi
Asynctask & Thread

Async Task class allows performing backgrond operations and


pblishing reslts on the !" thread withot ha#ing to maniplate
threads and/or handlers.

Async Task is designed to be a helper class arond Thread and $andler


and does not constitte a generic threading framework.

An asynchronos task is de%ned by a comptation that rns on a


backgrond thread and whose reslt is pblished on the !" thread.
Asynctask & Thread

AsyncTasks shold ideally be sed for short operations &a few


seconds at the most.' .

"f need to keep threads rnning for long periods of time( it is


highly recommended to se the #arios A)"s pro#ided by the
*a#a.til.concrrent pacakge sch as +xector(
Thread)ool+xector and ,treTask.
Asynctask & Thread

An asynchronos task is de%ned by - generic types( called


)arams( )rogress and .eslt( and / steps( called on)re+xecte(
do"n0ackgrond( on)rogress!pdate and on)ost+xecte.
Asynctask & Thread

An asynchronos task is de%ned by - generic types( called


)arams( )rogress and .eslt( and / steps( called on)re+xecte(
do"n0ackgrond( on)rogress!pdate and on)ost+xecte.

)arams1 the type of the inpt parameters sent to the task at


exection.

)rogress1 the type of the progress nits pblished dring the


backgrond comptation.
Asynctask & Thread

.eslt1 the type of the reslt of the backgrond comptation.


pri#ate class 2yTask extends AsyncTask 3)arams( )rogress(
.eslt45..6

Not all types are always sed by an asynchronos task. To mark


a type as nsed( simply se the type #oid
pri#ate class 2yTask extends AsyncTask37oid( 7oid( 7oid4
5 ... 6
Asynctask & Thread

AsyncTask class mst be sb-classed to be sed. The sbclass


will o#erride at least one method &do"n0ackgrond&)arams...''(
and most often will o#erride a second one
&on)ost+xecte&.eslt'.'

0asically there is for methods in AsyncTask these are


on)re+xecte&'( do"n0ackgrond&)arams...'(
on)rogress!pdate&)rogress...'( on)ost+xecte&.eslt'(.
Asynctask & Thread

This method is in#oked on the !" thread immediately after the


task is exected. This step is normally sed to setp the task(
for instance by showing a progress bar in the ser interface
Asynctask & Thread

do"n0ackgrond&)arams...'( in#oked on the backgrond thread


immediately after on)re+xecte&' %nishes execting.

This step is sed to perform backgrond comptation that can


take a long time. The parameters of the asynchronos task are
passed to this step.

The reslt of the comptation mst be retrned by this step


and will be passed back to the last step.
Asynctask & Thread

This step can also se pblish)rogress&)rogress...' to pblish


one or more nits of progress. These #ales are pblished on
the !" thread( in the on)rogress!pdate&)rogress...' step.

on)rogress!pdate&)rogress...'( in#oked on the !" thread after


call to pblish)rogress&)rogress...'. The timing of the exection
is nde%ned. This method is sed to display any form of
progress in the ser interface while the backgrond
comptation is still execting.
Asynctask & Thread

,or instance( it can be sed to animate a progress bar or show


logs in a text %eld.

on)ost+xecte&.eslt'( in#oked on the !" thread after the


backgrond comptation %nishes. The reslt of the backgrond
comptation is passed to this step as a parameter.
Thank 8o
Apex T9 "ndia )#t. :td. +-;< =ec->-
Noida
Apex TG India
E-20 , Sector 63, Noida
0120 4029000/9024/9025/902
!91-99535"454"
E#ai$ id% pratap&apext'i(co#
=tay ?onnected with s for more ))T on Android

You might also like