很多初入Android或Java开发的新手(我也在内)对Thread、Looper、Handler和Message仍然比较迷惑,衍生的有HandlerThread、java.util.concurrent、Task、AsyncTask由于目前市面上的书籍等资料都没有谈到这些问题,今天就这一问题做更系统性的总结。我们创建的Service、Activity以及Broadcast均是一个主线程处理,这里我们可以理解为UI线程。但是在操作一些耗时操作时,比如I/O读写的大文件读写,数据库操作以及网络下载需要很长时间,为了不阻塞用户界面,出现ANR的响应提示窗口,这个时候我们可以考虑使用Thread线程来解决。
首先使用一个实例来解释几个异步之间的关系:
实例内容:从网络上下载图片
此实例由Sundy讲解Android视频提供的,地址:http://www.verycd.com/topics/2900036/
XML代码:
Java代码:
public class LoadImageTest extends Activity{
private static final String TAG = "LoadImageTest";
private ImageView mImageView = null ;
//从网络上下载图片 .
private final String IMAGE1_URL = "http://image.91rb.com/200905/27/9/34b5b080ac80661657946eaa51566d03.jpg" ;
private final String IMAGE3_URL = "http://m.ztwan.com/wallpaper/UploadPic/2010/8/28/201082811538894.jpg" ;
private final String IMAGE4_URL = "http://m.ztwan.com/wallpaper/UploadPic/2010/8/28/20108280578236.jpg" ;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.load_imagetest) ;
mImageView = (ImageView)this.findViewById(R.id.imageThreadConcept) ;
/方法1、直接在UI线程中加载网络图片
findViewById(R.id.buttonWorkThread).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// 这只方式直接在网络中得到一张图片,因数据量比较小,在UI线程中执行,并不会造成用户视觉上的等待,如果数据量庞大,不采用这中方式
Drawable drawable = loadImageFromNetwork(IMAGE1_URL);
mImageView.setImageDrawable(drawable) ;
}}) ;
/方法2、java习惯,在android不推荐使用,使得使用线程不安全
findViewById(R.id.buttonWorkThread2).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// 在Android中是灰常不建议这样做的,这样做极易出现异常
new Thread(new Runnable(){
public void run() {
Drawable drawable = loadImageFromNetwork(IMAGE1_URL);
mImageView.setImageDrawable(drawable) ;
}
}).start() ;
}}) ;
//3. load image in new thread , but set imageview by View.post(Runnable)
//方法3、创建一个新的线程,( Runnable + Handler.postDelayed(runnable,time) )
findViewById(R.id.buttonWorkThread3).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
new Thread(new Runnable(){
Drawable drawable = loadImageFromNetwork(IMAGE3_URL);
public void run() {
mImageView.post(new Runnable(){
public void run() {
mImageView.setImageDrawable(drawable) ;
}}) ;
}
}).start() ;
}}) ;
//4. load image in new thread , but set imageview by AsyncTask
//方法4、使用AsyncTask,AsyncTask是在Android 1.5后引入的,能够更安全的使用线程,在下面,将会再用一个实例来分析AsyncTask
findViewById(R.id.buttonWorkThread4).setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// 不可缺少的异步,当数据量庞大时,耗时的操作时就使用这种方式吧。
//IMAGE4_URL 是执行传入的参数
new DownloadImageTask().execute(IMAGE4_URL) ;
}}) ;
}
//Async private class
private class DownloadImageTask extends AsyncTask {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
protected Drawable doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
protected void onPostExecute(Drawable result) {
mImageView.setImageDrawable(result);
}
}
// the Drawable loadImage main function
private Drawable loadImageFromNetwork(String imageUrl)
{
Drawable drawable = null;
try {
drawable = Drawable.createFromStream(
new URL(imageUrl).openStream(), "image.gif");
} catch (IOException e) {
Log.d(TAG, e.getMessage());
}
if (drawable == null) {
Log.d(TAG, "null drawable");
} else {
Log.d(TAG, "not null drawable");
}
return drawable ;
}
}
Java代码:
public class GeocodingDemoActivity extends MapActivity {
public static final String TAG = "GeocodingDemoActivity";
Geocoder geocoder = null;
MapController mMapController;
public MapView mapView;
ProgressDialog progDialog = null;
List addressList = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geocode);
int lat = (int) (31.83659536 * 1000000);
int lng = (int) (117.1912658 * 1000000);
mapView = (MapView) findViewById(R.id.geoMap);
mMapController = mapView.getController();
GeoPoint curpt = new GeoPoint(lat, lng);
mMapController.animateTo(curpt);
mapView.setBuiltInZoomControls(true);
mMapController.setZoom(15);
geocoder = new Geocoder(this);
Button geoBtn = (Button) findViewById(R.id.geocodeBtn);
geoBtn.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
EditText loc = (EditText) findViewById(R.id.location);
String locationName = loc.getText().toString();
progDialog = ProgressDialog.show(GeocodingDemoActivity.this,
"Processing...", "Finding Location...", true, false);
findLocation(locationName);
}
});
}
protected boolean isLocationDisplayed() {
return false;
}
protected boolean isRouteDisplayed() {
return false;
}
private void findLocation(final String locationName) {
//使用一个新的线程来实现地理位置的编码
Thread thrd = new Thread() {
public void run() {
try {
// do backgrond work
addressList = geocoder.getFromLocationName(locationName, 5);
// send message to handler to process results
uiCallback.sendEmptyMessage(0);
} catch (IOException e) {
e.printStackTrace();
}
}
};
thrd.start();
}
// ui thread callback handler
private Handler uiCallback = new Handler() {
@Override
public void handleMessage(Message msg) {
progDialog.dismiss();
//打印出本线程的ID
Log.i(TAG, "Handler Thread :" + Thread.currentThread().getId());
if (addressList != null && addressList.size() > 0) {
int lat = (int) (addressList.get(0).getLatitude() * 1000000);
int lng = (int) (addressList.get(0).getLongitude() * 1000000);
GeoPoint pt = new GeoPoint(lat, lng);
mapView.getController().setZoom(15);
mapView.getController().setCenter(pt);
} else {
Dialog foundNothingDlg = new AlertDialog.Builder(
GeocodingDemoActivity.this).setIcon(0)
.setTitle("Failed to Find Location")
.setPositiveButton("Ok", null)
.setMessage("Location Not Found...").create();
foundNothingDlg.show();
}
}
};
}
下面详解 AsyncTask的使用,学习一个方法前,应该很仔细的看官方的文档,这样我们会对它的机制更熟悉一些,再参考一些具体的实例,加深理解,
下面的一个实例使用AsyncTask加载一个网页的内容,并可有进度条显示加载的进度。实例来源于网络,作者还没找到:
XML代码:
Java代码:
public class AsyncTaskTset extends Activity {
private static final String TAG = "ASYNC_TASK";
private static final String URL = "http://www.google.com.hk/";
private Button execute;
private Button cancel;
private ProgressBar progressBar;
private TextView textView;
private MyTask mTask;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.asynctask);
execute = (Button) findViewById(R.id.execute);
execute.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//注意每次需new一个实例,新建的任务只能执行一次,否则会出现异常
mTask = new MyTask();
mTask.execute(URL);
execute.setEnabled(false);
cancel.setEnabled(true);
}
});
cancel = (Button) findViewById(R.id.cancel);
cancel.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//取消一个正在执行的任务,onCancelled方法将会被调用
mTask.cancel(true);
}
});
progressBar = (ProgressBar) findViewById(R.id.progress_bar);
textView = (TextView) findViewById(R.id.text_view);
}
//三个参数
/**
* 1、Params 启动任务执行的输入参数,比如HTTP请求的URL
* 2、Progress 后台任务执行的百分比
* 3、Result 后台执行任务最终返回的结果,比如String,也可以是一个image
*/
private class MyTask extends AsyncTask {
//onPreExecute方法用于在执行后台任务前做一些UI操作
protected void onPreExecute() {
Log.i(TAG, "onPreExecute() called");
textView.setText("loading...");
}
//doInBackground方法内部执行后台任务,不可在此方法内修改UI
protected String doInBackground(String... params) {
Log.i(TAG, "doInBackground(Params... params) called");
try {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(params[0]);
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
HttpEntity entity = response.getEntity();
InputStream is = entity.getContent();
long total = entity.getContentLength();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int count = 0;
int length = -1;
while ((length = is.read(buf)) != -1) {
baos.write(buf, 0, length);
count += length;
//调用publishProgress公布进度,最后onProgressUpdate方法将被执行
publishProgress((int) ((count / (float) total) * 100));
//为了演示进度,休眠500毫秒
Thread.sleep(500);
}
return new String(baos.toByteArray(), "gb2312");
}
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
return null;
}
//onProgressUpdate方法用于更新进度信息
protected void onProgressUpdate(Integer... progresses) {
Log.i(TAG, "onProgressUpdate(Progress... progresses) called");
progressBar.setProgress(progresses[0]);
textView.setText("loading..." + progresses[0] + "%");
}
//onPostExecute方法用于在执行完后台任务后更新UI,显示结果
protected void onPostExecute(String result) {
Log.i(TAG, "onPostExecute(Result result) called");
textView.setText(result);
execute.setEnabled(true);
cancel.setEnabled(false);
}
//onCancelled方法用于在取消执行中的任务时更改UI
protected void onCancelled() {
Log.i(TAG, "onCancelled() called");
textView.setText("cancelled");
progressBar.setProgress(0);
execute.setEnabled(true);
cancel.setEnabled(false);
}
}
}
本文转自:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=102664&fromuid=469532