Android Threading Mechanism--asynctask

Source: Internet
Author: User
<span id="Label3"></p><p><p>For Android Why use multi-threading, Because from the Android4.0, Google has established that the network operation is not allowed in the main thread to execute, thus there is a multi-threaded mechanism, there is a Java learning experience of the friend must know what multithreading refers to, in short, in the Java program, main () The function is opened as the main thread of the program, and we do not want to affect the execution of the main thread in order to complete some time-consuming operations, which is often done through the thread object creating a child Thread. Simply put, a program has only one main thread and can have multiple main threads. In the Android world, too, Android is a single-threaded model, and time-consuming operations must be performed in a non-main thread, so Google provides us with a asynctask multi-threaded operation object to facilitate our use of THREADS.</p></p><p><p>For Android to use threads there is a point to pay special attention to, which is that Android does not allow the UI to update the child thread, I believe many beginners must have encountered this problem, how to solve it? In the activity, we can</p></p><pre><pre> <span style="color: #0000ff;">new </span> Thread (<span style="color: #0000ff;">new </span> <span style="color: #000000; "> Runnable () {@Override </span> <span style="color: #0000ff;">public </span> <span style="color: #0000ff;">void </span> <span style="color: #000000;" "> run () {log.v (</span>" ABC "," sub-thread execution "<span style=" color: #000000; ">); Runonuithread (</span> <span style="color: #0000ff;">new </span> <span style="color: #000000;"> Runnable () { @Override </span> <span style="color: #0000ff;">public </span> <span style="color: #0000ff; ">void </span> <span style="color: #000000;" "> run () {log.v (</span>" ABC "," return to main thread execution "<span style=" color: #000000; ">); } }); }}). Start (); </span> </pre></pre><p><p>To achieve our results, but one thing to note is that in fragement, Runonuithread () can not be used, so you can use a little Attention. Of course, Android We can also through the Handler+messager to complete the multi-threaded operation, for the use of here, in the previous blog has been introduced for everyone, not to repeat, the following we began to introduce the focus of this article: Asynctask Use.</p></p><p><p>asynctask<parans, Progress, result> is an abstract class, we need to implement this abstract class first, before we can use, for its three parameters: parans: the type of the parameter entered when starting the task ; progress: the type of return value in background task execution; result: the type of results returned after the background execution of the task is Completed.</p></p><p><p>Constructs the callback method of the Asynctask subclass: 1, doinbackground: must rewrite, asynchronously executes the task that the background thread is going to complete, 2, Onpreexecute: is called before performing the background time-consuming operation, through the user completes some initialization operation; 3. Onpostexecute: when Doinbackground () is complete, the system automatically calls and passes the return value of Doinbackground () after execution to the OnPostExecute () method, which simply means, Doinbackground () completes the time-consuming operation and results in a OnPostExecute () method update ui;4, onprogressupdate: in the Doinbackground () method, call the Publishprogress () method , the method is triggered when the Task's execution progress is Updated.</p></p><p><p>Said so much, I believe you have some doubts in mind, below we through two simple Android applet, for everyone to specifically introduce how Asynctask use:</p></p><p><p>a, Asynctask is an abstract method of execution Order:</p></p><p><p>1. Create a asynctask sub-class object:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Myasynctask<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>asynctask<void, Void, void><span style="color: #000000;"><span style="color: #000000;">{@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnPreExecute () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnPreExecute (); LOG.V (</span></span>"abc", "onpreexecute"<span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span><span style="color: #000000;"><span style="color: #000000;">void Doinbackground (void ... arg0) {log.v (</span></span>"abc", "doinbackground"<span style="color: #000000;"><span style="color: #000000;">); Publishprogress (arg0); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnPostExecute (Void Result) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnPostExecute (result); LOG.V (</span></span>"abc", "onpostexecute"<span style="color: #000000;"><span style="color: #000000;">); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onprogressupdate (Void ... values) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. Onprogressupdate (values); LOG.V (</span></span>"abc", "onprogressupdate"<span style="color: #000000;"><span style="color: #000000;">); }}</span></span></pre><p><p>2. Call the child thread in mainactivity to Execute:</p></p><pre><pre><span style="color: #0000ff;"></span> public <span style="color: #0000ff;">class</span> <span style="color: #0000ff;">extends</span> <span style="color: #000000;">Activity { @Override </span><span style="color: #0000ff;">protected</span><span style="color: #0000ff;">void</span> <span style="color: #000000;"> onCreate (Bundle Savedinstancestate) { </span> <span style="color: #0000ff;">Super</span> <span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main); </span> <span style="color: #0000ff;">New</span> Myasynctask (). Execute (); <span style="color: #008000;">//</span> <span style="color: #008000;">Start Execution</span> <span style="color: #000000;">}</span></pre></pre><p><p>3, after the execution of the printed log log:</p></p><p><p></p></p><p><p>Through this log information, is not to solve the doubts in your mind, below we see a use asynctask load network pictures example.</p></p><p><p>b, Load the network image:</p></p><p><p>1. First create an XML that hosts the activity layout file:</p></p><pre><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"<span style="color: #000000;"><span style="color: #000000;">Xmlns:tools</span></span>= "http://schemas.android.com/tools"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_margin</span></span>= "10dp"<span style="color: #000000;"><span style="color: #000000;">Tools:context</span></span>=". Mainactivity "> <<span style="color: #000000;"><span style="color: #000000;">ImageView Android:id</span></span>= "@+id/img"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "match_parent"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "match_parent"/> <<span style="color: #000000;"><span style="color: #000000;">ProgressBar Android:id</span></span>= "@+id/progressbar"<span style="color: #000000;"><span style="color: #000000;">Android:layout_width</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">Android:layout_height</span></span>= "wrap_content"<span style="color: #000000;"><span style="color: #000000;">android:visibility</span></span>= "gone"<span style="color: #000000;"><span style="color: #000000;">android:layout_centerinparent</span></span>= "true"/></relativelayout></pre><p><p>Layout file is very simple, is a imageview+progressbar, in loading ImageView is we through ProgressBar to remind users to Wait.</p></p><p><p>2. Create our Activity object:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"></span> public</span> <span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Asynctaskimager<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span><span style="color: #000000;"><span style="color: #000000;">Activity {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">ProgressBar pb; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span><span style="color: #000000;"><span style="color: #000000;">ImageView image; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Static</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">Final</span></span>String URL = "https://www.baidu.com/img/bdlogo.png"<span style="color: #000000;"><span style="color: #000000;">; @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">onCreate (Bundle Savedinstancestate) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnCreate (savedinstancestate); Setcontentview (r.layout.imager); Init (); </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">imageasynctask (). Execute (url); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Private</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">Init () {PB</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ProgressBar) Findviewbyid (r.id.progressbar); Image</span></span>=<span style="color: #000000;"><span style="color: #000000;">(ImageView) Findviewbyid (r.id.img); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">class</span></span>Imageasynctask<span style="color: #0000ff;"><span style="color: #0000ff;">extends</span></span>asynctask<string, Void, bitmap><span style="color: #000000;"><span style="color: #000000;">{@Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnPreExecute () {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnPreExecute (); Pb.setvisibility (view.visible); } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span><span style="color: #000000;"><span style="color: #000000;">Bitmap doinbackground (String ... params) {Bitmap Bitmap</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; URLConnection Conn</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">NULL</span></span><span style="color: #000000;"><span style="color: #000000;">; String URL</span></span>= Params[0<span style="color: #000000;"><span style="color: #000000;">]; </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Try</span></span><span style="color: #000000;"><span style="color: #000000;">{conn</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">URL (url). openconnection (); InputStream</span> in</span>=<span style="color: #000000;"><span style="color: #000000;">Conn.getinputstream (); Bufferedinputstream bis</span></span>=<span style="color: #0000ff;"><span style="color: #0000ff;">New</span></span><span style="color: #000000;"><span style="color: #000000;">Bufferedinputstream (in); Bitmap</span></span>=<span style="color: #000000;"><span style="color: #000000;">Bitmapfactory.decodestream (bis); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(malformedurlexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Catch</span></span><span style="color: #000000;"><span style="color: #000000;">(ioexception E) {e.printstacktrace (); } </span></span><span style="color: #0000ff;"><span style="color: #0000ff;">return</span></span><span style="color: #000000;"><span style="color: #000000;">bitmap; } @Override</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">protected</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">void</span></span><span style="color: #000000;"><span style="color: #000000;">OnPostExecute (Bitmap Result) {</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">Super</span></span><span style="color: #000000;"><span style="color: #000000;">. OnPostExecute (result); Pb.setvisibility (view.gone); Image.setimagebitmap (result); } } }</span></span></pre><p><p>Here our work has been basically completed, and finally do not forget to make a statement in the Androidmanifest.xml.</p></p><p><p>3. Add Network Operation permissions:</p></p><pre><pre><uses-permission android:name= "android.permission.INTERNET"/></pre></pre><p><p>Because we need to use a network connection, we need to add a network access in Androidmanifest.xml.</p></p><p><p>4, finally with a running result:</p></p><p><p>  </p></p><p><p>Here about the introduction of Asynctask can draw a full stop, we have any questions, welcome message Discussion. For my understanding of the shortcomings, welcome criticism Pointing. Thank you</p></p><p><p>Android Threading Mechanism--asynctask</p></p></span>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.