首先是页面中的内容,大致如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.terry.code0333.AsyncTaskTest">
<TextView
android:id="@+id/show"
android:text="@string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:layout_below="@+id/show"
android:layout_toRightOf="@+id/show"
android:layout_toEndOf="@+id/show"
android:layout_marginTop="149dp" />
</RelativeLayout>
后台Activity内容如下:
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.Button;
import android.widget.TextView;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
public class AsyncTaskTest extends Activity {
private TextView show;
private Button loadData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_async_task_test);
show = (TextView) findViewById(R.id.show);
loadData = (Button) findViewById(R.id.button);
loadData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DownTask task = new DownTask(AsyncTaskTest.this);
try {
task.execute(new URL("https://github.com/onecoders/AndroidBook"));
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
});
}
class DownTask extends AsyncTask<URL, Integer, String> {
ProgressDialog pDialog;
int hasRead = 0;
Context mContext;
public DownTask(Context context) {
mContext = context;
}
@Override
protected String doInBackground(URL... params) {
StringBuilder sb = new StringBuilder();
try {
URLConnection conn = params[0].openConnection();
//打开conn连接对应的输入流,并将它包装成BufferedReader;
BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "utf-8"));
String line = null;
while ((line = br.readLine()) != null) {
sb.append(line).append("\n");
hasRead++;
publishProgress(hasRead);
}
return sb.toString();
} catch (IOException ex) {
ex.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(String result) {
show.setText(result);
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]);
}
}
}