AsyncTask(非同步任務)講解-android的學習之旅(四十六),asynctask-android

來源:互聯網
上載者:User

AsyncTask(非同步任務)講解-android的學習之旅(四十六),asynctask-android
AsyncTask簡介Android的UI線程主要處理使用者的按鍵,觸屏和View的繪製等,不能在裡面處理耗時的操作,否則會出現ANR,因此耗時的操作要單獨開一個線程處理,但是新線程不能直接處理UI線程的View。


AsyncTask的使用方法



程式碼範例

本執行個體只有一個按鈕和文字框,點擊按鈕從網路下載資料,然後又在文字框顯示。

package peng.liu.test;import android.app.Activity;import android.app.ProgressDialog;import android.content.Context;import android.os.AsyncTask;import android.os.Bundle;import android.view.View;import android.widget.TextView;import java.io.BufferedReader;import java.io.InputStreamReader;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;public class MainActivity extends Activity{    private TextView tvAsync;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tvAsync = (TextView) findViewById(R.id.tvAsync);    }    public void download(View source) throws MalformedURLException {        DownTask downTask = new DownTask(this);        downTask.execute(new URL("http://www.carzyit.org/ethos.php"));    }    class DownTask extends AsyncTask<URL,Integer,String>{        ProgressDialog dialog;        int hasdata;        Context context;        public DownTask(Context ctx){            this.context = ctx;        }        @Override        protected String doInBackground(URL... urls) {            StringBuilder sb = new StringBuilder();            try {                URLConnection conn = urls[0].openConnection();                BufferedReader bf = new BufferedReader(new InputStreamReader(conn.getInputStream(),"utf-8"));                String line = null;                while ((line = bf.readLine()) != null){                    sb.append(line);                    hasdata++;                    publishProgress(hasdata);                }                return sb.toString();            }            catch (Exception e){                e.printStackTrace();            }            return null;        }        @Override        protected void onPostExecute(String s) {            tvAsync.setText(s);            dialog.dismiss();        }        @Override        protected void onPreExecute() {            dialog = new ProgressDialog(context);            dialog.setTitle("hello");            dialog.setCancelable(false);            dialog.setMessage("world");            dialog.setMax(202);            dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);            dialog.setIndeterminate(false);            dialog.show();        }        @Override        protected void onProgressUpdate(Integer... values) {            tvAsync.setText(hasdata+"");            dialog.setProgress(values[0]);        }    }}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.