標籤:
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 shb16 *17 */18 public class MainActivity extends Activity {19 private TextView tView;20 @Override21 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 block30 e.printStackTrace();31 }32 }33 public class TestAsyn extends AsyncTask<URL,Integer,String> {34 35 @Override36 protected String doInBackground(URL... arg0) {37 // TODO Auto-generated method stub38 return arg0[0].toString();39 }40 @Override41 protected void onPostExecute(String result) {42 // TODO Auto-generated method stub43 super.onPostExecute(result);44 MainActivity.this.tView.setText(result);45 }46 @Override47 protected void onPreExecute() {48 // TODO Auto-generated method stub49 super.onPreExecute();50 Log.d("msg","onPreex");51 }52 }53 }
Android非同步任務AsyncTask