Android-asynchronous Task introduction and face Test
Source: Internet
Author: User
<span id="Label3"></p><p><p><strong>android-asynchronous Tasks</strong><br><strong>What's a asynctask?</strong><br>To reduce the difficulty of developing asynchronous operations, Android combines handle and thread pools to provide Asynctask. Asynctask is a packaged background task class,<br>As the name implies is an asynchronous task, he has the ability to perform time-consuming operations in the background, and can<br>The benefits of synchronizing the progress of the execution with the UI</p></p><p><p>Because handle is actually a bridge between two threads, the data transfer is one-way<br>Handle mechanisms Such as:<br></p></p><p><p>And the asynctask mechanism is as follows:<br></p></p><p><p><strong>two how to use Asynctask</strong><br>Asynctask defines three generic type params,progress and result:<br>Params: an input parameter that initiates task execution, such as the URL of an HTTP request<br>Progress: Percentage of background task execution<br>Result: results returned by the background execution of the task, such as String</p></p><p><p><strong>three main methods of Asynctask:</strong><br><strong>Required Method:</strong><br>Doinbackground (Params ...)<br>Background execution, More time-consuming operations can be placed Here.<br>Note that it is not possible to manipulate the URL directly, This method is executed in the background thread, and the main task of completing the job<br>It usually takes a long time. You can call publicprogress during execution to update the progress bar</p></p><p><p>OnPostExecute (Result)<br>Equivalent to the way handle handles the ui, where the results of the Doinbackground method can be used to manipulate the UI<br>This method executes on the main thread, and the result of the execution is returned as a value of this method's parameters</p></p><p><p><strong>Optional Methods:</strong><br>Onprogressupdate (Progress ...)<br>You can use the progress bar to increase the user experience, which is performed on the main thread to show the progress dialog box</p></p><p><p>OnPreExecute ()<br>This is the interface that the end user calls execute, and the dialog box where the progress bar can be displayed when the task is executed before it starts calling the method</p></p><p><p>Oncanceled ()<br>Action to be made when the user calls Cancel</p></p><p><p>Three states of Asynctask: Pending,runing,finish</p></p><p><p><strong>four asynctask principle of implementation</strong><br><strong>Why 1,asynctask must be created in the main thread:</strong><br>Because it's going to use Shandle.<br>private static final Internalhandler Shandler = new Internalhandler ();<br>The variable is a static variable, which is established before each use, so if it is not set to be created in the main thread,<br>will produce a lot of tasks, and when the system returns, it is not sure which one to return</p></p><p><p><strong>2,execute can only be called once and must be executed in the main</strong> thread</p></p><p><p>**3, do not call **onpreexecute (), onpostexecute (), doinbackground (), onprogressupdate () These methods<br>Called by the system itself<br>Attach Asynctask Source Code:<br>http://pan.baidu.com/s/1hq6c95i<br><strong>4, Use the occasion</strong> :<br>Use Asynctask: tasks can be terminated and need to be kept and used<br>Using handle: tasks need to be repeated multiple times and with less interaction with the UI</p></p><p><p>Does the quad detector need to use multithreading or Asynctask<br>Commonly used classes such as:<br></p></p><p><p>The detection code is as follows, and the result is a program launch popup dialog box</p></p><pre class="prettyprint"><code class=" hljs java"><span class="hljs-keyword"><span class="hljs-keyword"></span> package</span>com.chengzhi.androidstrictmode;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>java.io.OutputStream;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Build;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.Bundle;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.StrictMode;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.os.StrictMode.ThreadPolicy.Builder;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.annotation.SuppressLint;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.annotation.TargetApi;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.app.Activity;<span class="hljs-keyword"><span class="hljs-keyword">Import</span></span>android.view.Menu;<span class="hljs-annotation"><span class="hljs-annotation">@TargetApi</span></span>(build.version_codes. Gingerbread)<span class="hljs-annotation"><span class="hljs-annotation">@SuppressLint</span></span>(<span class="hljs-string"><span class="hljs-string">"newapi"</span></span>)<span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-class"><span class="hljs-class"> <span class="hljs-keyword">class</span> <span class="hljs-title">mainactivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Activity</span>{</span></span> <span class="hljs-comment"><span class="hljs-comment">//developer Mode</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Static</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Final</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span>Developer_mode =<span class="hljs-keyword"><span class="hljs-keyword">true</span></span>;<span class="hljs-annotation"><span class="hljs-annotation">@TargetApi</span></span>(build.version_codes. Gingerbread)<span class="hljs-annotation"><span class="hljs-annotation">@SuppressLint</span></span>(<span class="hljs-string"><span class="hljs-string">"newapi"</span></span>)<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword">protected</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">onCreate</span></span>(Bundle Savedinstancestate) {<span class="hljs-comment"><span class="hljs-comment">//detection Code</span></span> <span class="hljs-keyword"><span class="hljs-keyword">if</span></span>(developer_mode) {strictmode.setthreadpolicy (<span class="hljs-keyword"><span class="hljs-keyword">New</span></span>StrictMode.ThreadPolicy.Builder (). detectdiskreads (). detectdiskwrites () . detectnetwork (). Penaltydialog (). Build ());<span class="hljs-comment"><span class="hljs-comment">//this code can also</span></span> <span class="hljs-comment"><span class="hljs-comment">/*strictmode.threadpolicy _policy = new StrictMode.ThreadPolicy.Builder (). detectall (). penaltydeath (). Detec Tdiskreads (). detectdiskwrites (). detectnetwork (). Build (); Strictmode.setthreadpolicy (_policy); */</span></span>}<span class="hljs-keyword"><span class="hljs-keyword">Super</span></span>. onCreate (savedinstancestate); Processioaction (); }<span class="hljs-comment"><span class="hljs-comment">//creating A file stream in the main thread throws an Strictmode exception</span></span> <span class="hljs-keyword"><span class="hljs-keyword">Private</span></span> <span class="hljs-keyword"><span class="hljs-keyword">void</span></span> <span class="hljs-title"><span class="hljs-title">processioaction</span></span>() {outputstream out =<span class="hljs-keyword"><span class="hljs-keyword">NULL</span></span>;<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{out = Openfileoutput (<span class="hljs-string"><span class="hljs-string">"test"</span></span>, mode_world_writeable); Out.write (<span class="hljs-number"><span class="hljs-number">0</span></span>); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(Exception E) {<span class="hljs-comment"><span class="hljs-comment">//todo:handle Exception</span></span>E.printstacktrace (); }<span class="hljs-keyword"><span class="hljs-keyword">finally</span></span>{<span class="hljs-keyword"><span class="hljs-keyword">Try</span></span>{out.close (); }<span class="hljs-keyword"><span class="hljs-keyword">Catch</span></span>(Exception E2) {<span class="hljs-comment"><span class="hljs-comment">//todo:handle Exception</span></span>} } }<span class="hljs-annotation"><span class="hljs-annotation">@Override</span></span> <span class="hljs-keyword"><span class="hljs-keyword"></span> public</span> <span class="hljs-keyword"><span class="hljs-keyword">Boolean</span></span> <span class="hljs-title"><span class="hljs-title">Oncreateoptionsmenu</span></span>(menu Menu) {<span class="hljs-comment"><span class="hljs-comment">//inflate The menu; this adds items to the action bar if it is Present.</span></span>Getmenuinflater (). Inflate (r.menu.main, menu);<span class="hljs-keyword"><span class="hljs-keyword">return</span></span> <span class="hljs-keyword"><span class="hljs-keyword">true</span></span>; } }</code></pre><p><p><strong>Interview Questions:</strong></p></p><p><p>1,handle and Asynctask who are more resource-intensive<br>Asynctask Extra Handle</p></p><p><p>Asynctask:handle thread pool (creates a thread pool more than Handle)<br>Handle:handle<br>So Asynctask memory takes up more</p></p><p><p>is 2,asynctask multi-threaded?<br>no, See the previous Definition.</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: Welcome to exchange, QQ872785786</p></p> <p><p>Android-asynchronous Task introduction and face Test</p></p></span>
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