Android非同步任務AsyncTask

來源:互聯網
上載者:User

標籤:

 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

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.