文件異步下載

1. MainActivityphp

public class MainActivity extends AppCompatActivity {

    private TextView show;
    private Button mbut;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        show = (TextView)findViewById(R.id.show);
        mbut = (Button)findViewById(R.id.mbut);
        mbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    download(v);
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void download(View Source) throws MalformedURLException {
        DownTask task = new DownTask(this);
        task.execute(new URL("http://www.crazyit.org/ethos.php"));
    }

    private class DownTask extends AsyncTask<URL,Integer,String>{
        //可變長的輸入參數,與AsyncTask.exucute()對應
        ProgressDialog pdialog;
        //定義已經記錄讀取的行數
        int hasRead = 0;
        Context mContext;
        public DownTask(Context ctx) {
            mContext = ctx;
        }

        @Override
        protected String doInBackground(URL... params) {
            StringBuilder sb = new StringBuilder();
            try {
                //打開conn鏈接對應的輸入流,並將它包裝成BufferedReader
                URLConnection conn = params[0].openConnection();
                BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(),
                        "utf-8"));
                String line = null;
                while((line = br.readLine()) != null) {
                    sb.append(line + "\n");
                    hasRead ++;
                    publishProgress(hasRead);
                }
                return sb.toString();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String s) {
            //返回HTML頁面內容
            show.setText(s);
            pdialog.dismiss();
        }

        @Override
        protected void onPreExecute() {
            pdialog = new ProgressDialog(mContext);
            pdialog.setTitle("任務正在執行中...");
            pdialog.setMessage("任務正在執行中,請稍等...");
            //不能使用取消按鈕
            pdialog.setCancelable(false);
            //設置進度條的最大進度值
            pdialog.setMax(202);
            pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            //設置對話框的進度條是否顯示進度
            pdialog.setIndeterminate(false);
            pdialog.show();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            show.setText("已經讀取了【"+values[0]+"】行!");
            pdialog.setProgress(values[0]);
        }
    }
  }

2.本程序須要訪問網絡資源,所以還須要在AndroidMainfest.xml文件中聲明如下權限:java

<uses-permission android:name="android.permission.INTERNET">
</uses-permission>
相關文章
相關標籤/搜索