Android AsyncTask實現(xiàn)異步處理任務的方法詳解
Android AsyncTask實現(xiàn)異步處理任務的方法詳解
在開發(fā)Android應用時必須遵守單線程模型的原則:Android UI操作并不是線程安全的并且這些操作必須在UI線程中執(zhí)行。
Android 單線程模型概念詳解:http://www.dbjr.com.cn/article/112165.htm
在單線程模型中始終要記住兩條法則:
不要阻塞UI線程
確保只在UI線程中訪問Android UI工具包
當一個程序第一次啟動時,Android會同時啟動一個對應的主線程(Main Thread),主線程主要負責處理與UI相關(guān)的事件,如:用戶的按鍵事件,用戶接觸屏幕的事件以及屏幕繪圖事件,并把相關(guān)的事件分發(fā)到對應的組件進行處理。所以主線程通常又被叫做UI線程。
比如說從網(wǎng)上獲取一個網(wǎng)頁,在一個TextView中將其源代碼顯示出來,這種涉及到網(wǎng)絡操作的程序一般都是需要開一個線程完成網(wǎng)絡訪問,但是在獲得頁面源碼后,是不能直接在網(wǎng)絡操作線程中調(diào)用TextView.setText()的.因為其他線程中是不能直接訪問主UI線程成員
Android提供了幾種在其他線程中訪問UI線程的方法。
Activity.runOnUiThread( Runnable ) View.post( Runnable ) View.postDelayed( Runnable, long ) Hanlder
這些類或方法同樣會使你的代碼很復雜很難理解。然而當你需要實現(xiàn)一些很復雜的操作并需要頻繁地更新UI時這會變得更糟糕。
為了解決這個問題,Android 1.5提供了一個工具類:AsyncTask,它使創(chuàng)建需要與用戶界面交互的長時間運行的任務變得更簡單。不需要借助線程和Handler即可實現(xiàn)。
AsyncTask是抽象類.AsyncTask定義了三種泛型類型 Params,Progress和Result。
◆Params 啟動任務執(zhí)行的輸入?yún)?shù),比如HTTP請求的URL。
◆Progress 后臺任務執(zhí)行的百分比。
◆Result 后臺執(zhí)行任務最終返回的結(jié)果,比如String。
AsyncTask的執(zhí)行分為四個步驟,每一步都對應一個回調(diào)方法,這些方法不應該由應用程序調(diào)用,開發(fā)者需要做的就是實現(xiàn)這些方法。
子類化AsyncTask
實現(xiàn)AsyncTask中定義的下面一個或幾個方法
onPreExecute(), 該方法將在執(zhí)行實際的后臺操作前被UI thread調(diào)用??梢栽谠摲椒ㄖ凶鲆恍蕚涔ぷ?,如在界面上顯示一個進度條。
doInBackground(Params...), 將在onPreExecute 方法執(zhí)行后馬上執(zhí)行,該方法運行在后臺線程中。這里將主要負責執(zhí)行那些很耗時的后臺計算工作??梢哉{(diào)用 publishProgress方法來更新實時的任務進度。該方法是抽象方法,子類必須實現(xiàn)。
onProgressUpdate(Progress...),在publishProgress方法被調(diào)用后,UI thread將調(diào)用這個方法從而在界面上展示任務的進展情況,例如通過一個進度條進行展示。
onPostExecute(Result), 在doInBackground 執(zhí)行完成后,onPostExecute 方法將被UI thread調(diào)用,后臺的計算結(jié)果將通過該方法傳遞到UI thread.
為了正確的使用AsyncTask類,以下是幾條必須遵守的準則:
1) Task的實例必須在UI thread中創(chuàng)建
2) execute方法必須在UI thread中調(diào)用
3) 不要手動的調(diào)用onPreExecute(), onPostExecute(Result),doInBackground(Params...), onProgressUpdate(Progress...)這幾個方法
4) 該task只能被執(zhí)行一次,否則多次調(diào)用時將會出現(xiàn)異常
從網(wǎng)上獲取一個網(wǎng)頁,在一個TextView中將其源代碼顯示出來
package test.list;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.util.ArrayList;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class NetworkActivity extends Activity{
private TextView message;
private Button open;
private EditText url;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.network);
message= (TextView) findViewById(R.id.message);
url= (EditText) findViewById(R.id.url);
open= (Button) findViewById(R.id.open);
open.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
connect();
}
});
}
private void connect() {
PageTask task = new PageTask(this);
task.execute(url.getText().toString());
}
class PageTask extends AsyncTask<String, Integer, String> {
// 可變長的輸入?yún)?shù),與AsyncTask.exucute()對應
ProgressDialog pdialog;
public PageTask(Context context){
pdialog = new ProgressDialog(context, 0);
pdialog.setButton("cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int i) {
dialog.cancel();
}
});
pdialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
public void onCancel(DialogInterface dialog) {
finish();
}
});
pdialog.setCancelable(true);
pdialog.setMax(100);
pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdialog.show();
}
@Override
protected String doInBackground(String... params) {
try{
HttpClient client = new DefaultHttpClient();
// params[0]代表連接的url
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
HttpEntity entity = response.getEntity();
long length = entity.getContentLength();
InputStream is = entity.getContent();
String s = null;
if(is != null) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[128];
int ch = -1;
int count = 0;
while((ch = is.read(buf)) != -1) {
baos.write(buf, 0, ch);
count += ch;
if(length > 0) {
// 如果知道響應的長度,調(diào)用publishProgress()更新進度
publishProgress((int) ((count / (float) length) * 100));
}
// 讓線程休眠100ms
Thread.sleep(100);
}
s = new String(baos.toByteArray()); }
// 返回結(jié)果
return s;
} catch(Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onCancelled() {
super.onCancelled();
}
@Override
protected void onPostExecute(String result) {
// 返回HTML頁面的內(nèi)容
message.setText(result);
pdialog.dismiss();
}
@Override
protected void onPreExecute() {
// 任務啟動,可以在這里顯示一個對話框,這里簡單處理
message.setText(R.string.task_started);
}
@Override
protected void onProgressUpdate(Integer... values) {
// 更新進度
System.out.println(""+values[0]);
message.setText(""+values[0]);
pdialog.setProgress(values[0]);
}
}
}
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
相關(guān)文章
Android實現(xiàn)RecyclerView嵌套流式布局的詳細過程
最近在做需求的時候,碰到有各種篩選項的界面,下面這篇文章主要給大家介紹了關(guān)于Android實現(xiàn)RecyclerView嵌套流式布局的詳細過程,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下2022-12-12
android讀寫sd卡操作寫入數(shù)據(jù)讀取數(shù)據(jù)示例
這篇文章主要介紹了android讀寫sd卡操作,示例實現(xiàn)了寫入數(shù)據(jù)讀取數(shù)據(jù)的功能,大家參考使用吧2014-01-01
Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼
Android Bitmap和Drawable相互轉(zhuǎn)換的簡單代碼,需要的朋友可以參考一下2013-05-05
Android自定義狀態(tài)欄顏色與APP風格保持一致的實現(xiàn)方法
我們知道iOS上的應用,狀態(tài)欄的顏色總能與應用標題欄顏色保持一致,用戶體驗很不錯,那安卓是否可以呢?下面小編給大家?guī)砹薃ndroid自定義狀態(tài)欄顏色與APP風格保持一致的實現(xiàn)方法,跟著小編一起學習吧2016-10-10

