码迷,mamicode.com
首页 > 移动开发 > 详细

Android异步任务AsyncTask

时间:2015-09-21 09:10:13      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:

 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 }

 

Android异步任务AsyncTask

标签:

原文地址:http://www.cnblogs.com/assassin666/p/4825158.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!