标签:
1 package com.example.asynctask; 2 3 import java.net.MalformedURLException; 4 import java.net.URL; 5 6 import android.app.Activity; 7 import android.os.AsyncTask; 8 import android.os.Bundle; 9 import android.util.Log; 10 import android.view.Menu; 11 import android.view.MenuItem; 12 import android.widget.TextView; 13 /** 14 * 异步任务实现,可以实现直接修改UI线程的控件。 15 * @author shb 16 * 17 */ 18 public class MainActivity extends Activity { 19 private TextView tView; 20 @Override 21 protected void onCreate(Bundle savedInstanceState) { 22 super.onCreate(savedInstanceState); 23 setContentView(R.layout.activity_main); 24 tView = (TextView) this.findViewById(R.id.textView1); 25 TestAsyn testAsyn = new TestAsyn(); 26 try { 27 testAsyn.execute(new URL("https://www.baidu.com/img/bd_logo1.png")); 28 } catch (MalformedURLException e) { 29 // TODO Auto-generated catch block 30 e.printStackTrace(); 31 } 32 } 33 public class TestAsyn extends AsyncTask<URL,Integer,String> { 34 35 @Override 36 protected String doInBackground(URL... arg0) { 37 // TODO Auto-generated method stub 38 return arg0[0].toString(); 39 } 40 @Override 41 protected void onPostExecute(String result) { 42 // TODO Auto-generated method stub 43 super.onPostExecute(result); 44 MainActivity.this.tView.setText(result); 45 } 46 @Override 47 protected void onPreExecute() { 48 // TODO Auto-generated method stub 49 super.onPreExecute(); 50 Log.d("msg","onPreex"); 51 } 52 } 53 }
标签:
原文地址:http://www.cnblogs.com/assassin666/p/4825158.html