Android. OS. asynctask

Source: Internet
Author: User
Introduction asynctask makes it easier and more appropriate to use the UI thread. It can run some operations in the background and then display them on the UI, there is no need to operate on a specific thread or handlers. An asynchronous task consists of three basic types (call parameters, progress, and result), and four steps (call Start, Run in the background, process progress, end), and most often will override a second one (onpostexecute (result ).) the usage description of asynchronous task must be used as a subclass, and the task instance must create execute (Params...) in the UI thread ...) do not manually call onpreexecute (), onpostexecute (result), doinbackground (Params ...), onprogressupdate (Progress ...). Task can only execute once. If an exception is reported after multiple executions, the definition of private class downloadfilestask extends asynctask <URL, integer, long> {protected long doinbackground (URL... URLs) {int COUNT = URLs. length; long totalsize = 0; For (INT I = 0; I <count; I ++) {totalsize + = Downloader. downloadfile (URLs [I]); publishprogress (INT) (I/(float) count) * 100);} return totalsize;} protected void onprogressupdate (integer... progress) {setprogresspercent (Progress [0]);} protected void onpostexecute (long result) {showdialog ("downloaded" + Result + "bytes ");}} class use new downloadfilestask(cmd.exe cute (url1, url2, url3); three basic types of description Params, the type of parameters passed to the task progress, indicating the type result of the Progress unit, not all tasks of the return type need to define the type. If not, use void, as shown below: private class mytask extends asynctask <void, void, void> {...} onpreexecute (): Call doinbackground (Params...) immediately after the task is executed ...): After onpreexecute is executed, run this method. The parameter is passed to this method. After the execution is completed, a value must be returned. You can also use publishprogress (Progress ...) release progress to onprogressupdate (Progress ...), onprogressupdate (Progress ...): publishprogress (Progress ...) after the method is called, the method is executed. The progress information is usually displayed as a progress bar, or the log information onpostexecute (result) is displayed in the text field when doinbackground (Params ...) example of an instance in the real-name OCR function of mobile phones after the method is executed: Class ocrtask extends asynctask <string, void, ocrmessage >{@ override protected ocrmessage doinbackground (string... arg0) {applyservice service = new applyservice (getapplicationcontext (); return service. ocrtest (save_file_path) ;}@ override protected void onpostexecute (ocrmessage result) {returnimagescan (result) ;}} call ocrtask OCR = new effeccute (save_file_path); Example 2: Save image tasks, the parameter is a byte array, which is the byte array class savephototask extends asynctask <byte [], void, integer> {string picname; @ override protected integer doinbackground (byte []... datas) {byte [] DATA = datas [0]; Bitmap bitmap; bitmapfactory. options OPS2 = new bitmapfactory. options (); ops2.insamplesize = 1; bitmap = bitmapfactory. decodebytearray (data, 0, Data. length, OPS2); try {fileoutputstream Fos = ocrcameraactivity. this. openfileoutput ("output.jpg", activity. mode_world_readable); bitmap. compress (bitmap. compressformat. JPEG, 80, FOS); FOS. flush (); FOS. close (); save_file_path = "output.jpg";} catch (exception e) {e. printstacktrace () ;}return 0 ;}@ override protected void onpostexecute (integer result) {call_ocr () ;}} call savephototask = new savephototask(savephototask.exe cute (data );

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.