|
| 1 | +package com.example.asynctasktest; |
| 2 | + |
| 3 | +import java.io.BufferedReader; |
| 4 | +import java.io.IOException; |
| 5 | +import java.io.InputStreamReader; |
| 6 | +import java.net.MalformedURLException; |
| 7 | +import java.net.URL; |
| 8 | +import java.net.URLConnection; |
| 9 | + |
| 10 | +import android.app.Activity; |
| 11 | +import android.app.ProgressDialog; |
| 12 | +import android.content.Context; |
| 13 | +import android.os.AsyncTask; |
| 14 | +import android.os.Bundle; |
| 15 | +import android.view.Menu; |
| 16 | +import android.view.View; |
| 17 | +import android.view.View.OnClickListener; |
| 18 | +import android.widget.Button; |
| 19 | +import android.widget.TextView; |
| 20 | + |
| 21 | +public class AsyncTaskTest extends Activity { |
| 22 | + |
| 23 | + private TextView showResult; |
| 24 | + private Button loadData; |
| 25 | + |
| 26 | + @Override |
| 27 | + protected void onCreate(Bundle savedInstanceState) { |
| 28 | + super.onCreate(savedInstanceState); |
| 29 | + setContentView(R.layout.main); |
| 30 | + |
| 31 | + showResult = (TextView) findViewById(R.id.showResult); |
| 32 | + loadData = (Button) findViewById(R.id.loadData); |
| 33 | + |
| 34 | + loadData.setOnClickListener(new OnClickListener() { |
| 35 | + |
| 36 | + @Override |
| 37 | + public void onClick(View v) { |
| 38 | + DownLoadTask task = new DownLoadTask(AsyncTaskTest.this); |
| 39 | + try { |
| 40 | + task.execute(new URL("http://www.crazyit.org/ethos.php")); |
| 41 | + } catch (MalformedURLException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + class DownLoadTask extends AsyncTask<URL, Integer, String> { |
| 50 | + |
| 51 | + ProgressDialog pdialog; |
| 52 | + int hasRead; |
| 53 | + Context mContext; |
| 54 | + |
| 55 | + public DownLoadTask(Context ctx) { |
| 56 | + this.mContext = ctx; |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + protected String doInBackground(URL... params) { |
| 61 | + StringBuilder sb = new StringBuilder(); |
| 62 | + try { |
| 63 | + URLConnection conn = params[0].openConnection(); |
| 64 | + BufferedReader br = new BufferedReader(new InputStreamReader( |
| 65 | + conn.getInputStream(), "utf-8")); |
| 66 | + String line = null; |
| 67 | + while ((line = br.readLine()) != null) { |
| 68 | + sb.append(line + "\n"); |
| 69 | + hasRead++; |
| 70 | + publishProgress(hasRead); |
| 71 | + } |
| 72 | + return sb.toString(); |
| 73 | + } catch (IOException e) { |
| 74 | + e.printStackTrace(); |
| 75 | + } |
| 76 | + return null; |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + protected void onPostExecute(String result) { |
| 81 | + showResult.setText(result); |
| 82 | + pdialog.dismiss(); |
| 83 | + } |
| 84 | + |
| 85 | + @Override |
| 86 | + protected void onPreExecute() { |
| 87 | + pdialog = new ProgressDialog(mContext); |
| 88 | + pdialog.setTitle("Task doing..."); |
| 89 | + pdialog.setMessage("Task executing...Please wait..."); |
| 90 | + pdialog.setCancelable(false); |
| 91 | + pdialog.setMax(202); |
| 92 | + pdialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); |
| 93 | + pdialog.setIndeterminate(false); |
| 94 | + pdialog.show(); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + protected void onProgressUpdate(Integer... values) { |
| 99 | + showResult.setText("Have read " + values[0] + "ÐÐ"); |
| 100 | + pdialog.setProgress(values[0]); |
| 101 | + } |
| 102 | + |
| 103 | + } |
| 104 | + |
| 105 | + @Override |
| 106 | + public boolean onCreateOptionsMenu(Menu menu) { |
| 107 | + // Inflate the menu; this adds items to the action bar if it is present. |
| 108 | + getMenuInflater().inflate(R.menu.async_task_test, menu); |
| 109 | + return true; |
| 110 | + } |
| 111 | + |
| 112 | +} |
0 commit comments