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