Android--비동기 임무 AsyncTask


 인 스 턴 스:비동기 작업 을 사용 하여 네트워크 에서 다운로드 합 니 다.
package com.example.xiaocool.anysctaskdemo;

import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URLConnection;


public class MainActivity extends ActionBarActivity {
    private TextView show;
    private Button download;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = (TextView) this.findViewById(R.id.show_down);
        download = (Button) this.findViewById(R.id.button);
        download.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                DownTask task = new DownTask(MainActivity.this);
                try {
                    /**
                     *          
                     */
                    task.execute(new URI("http://news.baidu.com/"));
                } catch (Exception E) {
                    E.printStackTrace();
                }

            }
        });
    }


    /**
     * Params                
     * Progress              ,
     * Result                    
     *           Void
     * <p/>
     *            
     */
    class DownTask extends AsyncTask<URI, Integer, String> {

        ProgressDialog progressDialog;
        //           
        int hasRead = 0;
        Context mContext;

        public DownTask(Context context) {
            mContext = context;
        }

        /**
         *            ,       publishProgress(Progress value)         
         *
         * @param params
         * @return
         */
        @Override
        protected String doInBackground(URI... params) {
            StringBuilder sb = new StringBuilder();
            try {
                URLConnection conn = params[0].toURL().openConnection();
                //  conn        ,   BufferReader   
                BufferedReader br = new BufferedReader(
                        new InputStreamReader(conn.getInputStream(), "utf-8")
                );
                String line = null;
                while ((line = br.readLine()) != null) {
                    sb.append(line + "
");                     hasRead++;                     //                     publishProgress(hasRead);                 }                 return sb.toString();             } catch (Exception e) {                 e.printStackTrace();             }             return null;         }         /**          *  doInBackground publishProgress(Progress value), ,          *          * @param values          */         @Override         protected void onProgressUpdate(Integer... values) {             show.setText(" "+values[0]);             progressDialog.setProgress(values[0]);         }         /**          *  , ,          */         @Override         protected void onPreExecute() {             progressDialog = new ProgressDialog(mContext);             progressDialog.setTitle(" !");             progressDialog.setMessage(" , !!");             //             progressDialog.setCancelable(false);             progressDialog.setMax(202);             progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);             progressDialog.setIndeterminate(false);             progressDialog.show();         }         /**          *  doInBackground ,   doInBackground()          *          * @param s          */         @Override         protected void onPostExecute(String s) {             // HTML              show.setText(s);             progressDialog.dismiss();         }     } }

좋은 웹페이지 즐겨찾기