首先定義乙個類,繼承asynctask類,並實現相關方法:
/**
* @author wjl 第乙個泛型:規定doinbackground方法的引數型別,規定asynctask.execute方法傳遞的引數型別
* 第二個泛型:規定onprogressupdate方法引數的型別,publishprogress引數型別
* 第三個泛型:規定doinbackground方法的返回值型別
*/public class myasynctask extends asynctask
// runs on the ui thread before doinbackground.
@override
// 執行在主線程,在doinbackground方法之前執行
protected void onpreexecute()
@override
// 執行在子執行緒,做耗時操作
// 返回值被onpostexecute接收
protected string doinbackground(string... params)
} catch (clientprotocolexception e) catch (ioexception e)
return string;
} // runs on the ui thread after doinbackground.
// 執行在主線程,在doinbackground方法之後執行,接收doinbackground方法的返回值
在此方法內更新ui執行緒中的控制項
protected void onpostexecute(string result) ;
// 更新進度,執行在主線程上,在publishprogress執行之後
protected void onprogressupdate(double values) ;
//獲取進度
public void progress()
bufferedreader.close();
inputstream.close();
string = buffer.tostring();*/
} }
在ui主線程呼叫(注:建立乙個myasynctask物件,只能呼叫一次excute方法)
public class mainactivity extends activity catch (interruptedexception e) catch (executionexception e)
}}
AsyncTask 非同步處理
1,object,用於指定doinbackground的引數 2,integer,用於指定onprogressupdate的引數 3,uri,用於指定doinbackground的返回型別和onpostexecute的引數型別 public class updatetask extends asyn...
AsyncTask非同步任務
asynctask非同步任務怎麼寫 寫乙個類繼承asynctask,重寫方法 doinbackgroud 再new 這個類,執行物件 execute 在ui執行緒裡面寫這個類的實現方法 doinbackground有可能是新開的執行緒,有可能是執行緒池裡面的執行緒 執行順序 onpreexecute...
非同步任務(AsyncTask)
一 解決新執行緒無法更新ui組建問題的方案 為了解決新執行緒不能更新ui組建的問題,andorid提供了如下幾種解決方案 1.使用handler實現執行緒之間的通訊。mhandler new handler public class mythread extends thread 2.activit...