前面介紹了異步任務的處理,這里來學一下Handler和Message機制吧。下面這個代碼和之前的一樣,都是從網上下載圖片的。
~~~
package com.example.myasynctask;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpRequest;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button button;
private ImageView iv;
private String path = "http://cdn.duitang.com/uploads/item/201310/17/20131017194234_eZfFU.thumb.600_0.png";
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Bitmap bm = null;
byte[] data = (byte[])msg.obj;
bm = BitmapFactory.decodeByteArray(data, 0, data.length);
iv.setImageBitmap(bm);
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.bt);
iv = (ImageView) this.findViewById(R.id.iv);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new Thread(new myThread()).start();
}
});
}
private class myThread implements Runnable {
@Override
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(path);
InputStream inputStream = null;
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
if (httpResponse.getStatusLine().getStatusCode() == 200 ) {
HttpEntity httpEntity = httpResponse.getEntity();
byte[] data = EntityUtils.toByteArray(httpEntity);
Message message = Message.obtain();
message.obj = data;
handler.sendMessage(message);
}
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
}
~~~
~~~
~~~
~~~
~~~
Looper維護一個消息隊列(在loop()函數里面)
Message.obtain可回收的獲取Message對象(這樣就不用多次new對象)
Handler通過sendMessage來把Message進行消息隊列的入隊,并且把Message的target(也就是對應的Handler綁定為自己)
當消息隊列循環到相應的Message的時候,會調用Message綁定的Handler的dispatchMessage函數,該函數會調用handleMessage函數,也就是用戶實現的handleMessage函數。Handler可以綁定多個Message,通過Message.what來識別身份。
--------源碼所得,2014/ 9/ 9 修改
- 前言
- 安卓ListView一個簡單代碼的注釋和探討
- 安卓wifi熱點編程代碼的若干注釋
- 安卓距離傳感器編程
- 簡單的ScrollView
- 簡單的ListView
- 簡單的ArrayAdapter
- AsyncTask的初步學習
- AsyncTask再度學習
- Handler初步學習
- 開啟系統Activity
- 隱式調用Activity
- Activity的生命周期
- startActivityForResult的初步學習
- 多點觸控拉伸圖片
- LruCache圖片緩存技術
- SQLiteOpenHelper數據庫操作
- 短信攔截器
- 簡單的Notification
- ListView優化以及checkbox狀態問題
- 安卓多線程下載
- 安卓JSON解析初步探討
- 選項卡樣式的fragment
- DrawerLayout實現簡單的側滑功能
- 安卓軟引用解決圖片OOM問題
- 安卓隱式Intent啟動Activity和BroadcastReceiver若干注意點
- Dialog學習筆記
- ActionBar使用