使用异步任务下载--疯狂android讲义实例
来源:互联网 发布:八进制转十进制算法 编辑:程序博客网 时间:2024/06/07 11:00
package com.example.android_test;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.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class MainActivity extends Activity {TextView show;EditText editText;Button ok;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);show=(TextView) findViewById(R.id.show);editText=(EditText) findViewById(R.id.et);ok=(Button) findViewById(R.id.ok);ok.setOnClickListener(new OnClickListener() {public void onClick(View v) {DownTask task=new DownTask(MainActivity.this);try {task.execute(new URL("http://"+editText.getText().toString()));} catch (MalformedURLException e) {e.printStackTrace();}}});}class DownTask extends AsyncTask<URL, Integer, String>{ProgressDialog progressDialog;int hasRead=0;Context mContext;DownTask(Context context){mContext=context;}@Overrideprotected 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;}@Overrideprotected void onPostExecute(String result) {//返回html页面内容show.setText(result);progressDialog.dismiss();}@Overrideprotected void onPreExecute() {progressDialog=new ProgressDialog(mContext);progressDialog.setTitle("任务正在执行中..");progressDialog.setMessage("请等待...");progressDialog.setCancelable(false);progressDialog.setMax(100);progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);//不明确具体进度,动画效果progressDialog.setIndeterminate(false);progressDialog.show();}@Overrideprotected void onProgressUpdate(Integer... values) {show.setText("已经读取了【"+values[0]+"】行!");progressDialog.setProgress(values[0]);}}}
0 0
- 使用异步任务下载--疯狂android讲义实例
- 《Android 疯狂讲义》下载
- Android讲义3---第三章---异步任务
- Android使用异步任务下载图片
- 疯狂Android讲义
- 《疯狂Android讲义》读书笔记
- 疯狂android讲义---Configuration
- 疯狂android讲义---activity
- 《疯狂Android讲义》
- 疯狂android讲义目录
- 疯狂Android讲义
- 疯狂Android讲义源码
- 《疯狂Android讲义》
- 疯狂android讲义笔记
- 《疯狂Android讲义》目录
- 《疯狂Android讲义》
- Android 疯狂讲义学习
- 疯狂Android讲义(第二版)pdf及源码、工具下载
- (Caffe,LeNet)初始化测试网络(四)
- ubuntu下编写简单的helloworld程序
- java中静态变量和实例变量到底有什么区别
- android最佳实践指南
- ON_MESSAGE
- 使用异步任务下载--疯狂android讲义实例
- 枚举
- 数组I冒泡法排序
- 分布式服务框架 dubbo/dubbox 入门示例
- Elasticsearch - 短语匹配(match_phrase)以及slop参数
- java io(一)
- [IOS]Label-UIMenuController
- 21. Merge Two Sorted Lists | Java最短代码实现
- windows客户端开发--为你的客户端增加日志系统(log4cplus)