Android异步任务AsyncTask

 package com.example.asynctask;

 import java.net.MalformedURLException;
import java.net.URL; import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
/**
* 异步任务实现,可以实现直接修改UI线程的控件。
* @author shb
*
*/
public class MainActivity extends Activity {
private TextView tView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tView = (TextView) this.findViewById(R.id.textView1);
TestAsyn testAsyn = new TestAsyn();
try {
testAsyn.execute(new URL("https://www.baidu.com/img/bd_logo1.png"));
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class TestAsyn extends AsyncTask<URL,Integer,String> { @Override
protected String doInBackground(URL... arg0) {
// TODO Auto-generated method stub
return arg0[0].toString();
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
MainActivity.this.tView.setText(result);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
Log.d("msg","onPreex");
}
}
}
上一篇:深入理解Java虚拟机学习笔记(二)-----垃圾收集器与内存分配策略


下一篇:maven安装和配置