这里利用 ProgressBar 即时显示下载进度。

途中碰到的问题:

1、主线程中不能打开 URL,和只能在主线程中使用 Toast 等

2、子线程不能修改 UI

3、允许网络协议

4、暂停下载和继续下载

  ........

fragment_main 布局文件

 <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"
tools:context="com.dragon.android.textbar.MainActivity$PlaceholderFragment" > <!-- prigressBar 进度条 -->
<!-- progress 当前进度 -->
<!-- indeterminate 不明确的 默认false -->
<ProgressBar
android:id="@+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:max="100"
android:progress="0"
android:indeterminate="true"/> <Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="startLoad"
android:layout_marginTop="86dp"
android:background="#009FEE"
android:text="@string/start"
android:textColor="#ffffff" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/progressBar1"
android:background="@null"
android:layout_alignParentLeft="true" /> </RelativeLayout>

fragment_main

strings.xml

 <?xml version="1.0" encoding="utf-8"?>
<resources> <string name="app_name">hwdownload</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="start">开始</string>
<string name="stop">暂停</string>
<string name="contin">继续</string> </resources>

strings

(问题3)在 AndroidManifest 文件中配置

 <!-- 请求网络权限 -->
<uses-permission android:name="android.permission.INTERNET"/>

MainActivity(问题1、2)

package com.dragon.android.textbar;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast; /**
* 只有创建一个 View 的线程才可以改变这个 View 的UI!!! 主线程也叫 UI 线程
*/
public class MainActivity extends Activity {
private ProgressBar progressBar1;
private Button button1;
private TextView textView1; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main); progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
button1 = (Button) findViewById(R.id.button1);
textView1 = (TextView) findViewById(R.id.textView1); } public void startLoad(View view) {
String text = (String) button1.getText();
// 设置按钮内容 ----并没有用...
button1.setText(text.equals(getResources().getString(R.string.start)) ? R.string.stop
: (text.equals(getResources().getString(R.string.stop)) ? R.string.contin
: R.string.stop));
progressBar1.setIndeterminate(false); new Thread(new Runnable() {
private int percent; @Override
public void run() {
try {
// 打开 URL 必须在子线程
URL url = new URL(
"http://b.zol-img.com.cn/sjbizhi/images/9/540x960/1472549276394.jpg");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// conn.setRequestMethod("GET");
// conn.setReadTimeout(5000);
// conn.setConnectTimeout(5000); int contentLength = conn.getContentLength(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream(); byte[] buffer = new byte[1024];
int len = -1;
int sum = 0;
while ((len = is.read(buffer)) != -1) {
sum += len;
// 注意强转方式,防止一直为0
percent = (int) (100.0 * sum / contentLength);
// 在主线程上运行的子线程
runOnUiThread(new Runnable() { @Override
public void run() {
progressBar1.setProgress(percent);
textView1.setText(percent + "%");
if (percent == progressBar1.getMax()) {
Toast.makeText(MainActivity.this,
"下载完成!", Toast.LENGTH_SHORT)
.show();
}
}
});
}
is.close();
conn.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
}

**************然而并没有解决问题4,要用断点续传,但是还不会存放assets资源.....***************

 

最新文章

  1. 理解linux and inode
  2. viewTemplate
  3. asp.net MVC 自动下载apk
  4. 第一章 搭建Qt开发环境
  5. Boost format
  6. Google Test Frame 简单使用例子
  7. ubuntu 包维护
  8. H面试程序(4):翻转句子中单词的顺序 .
  9. 升级iOS8系统后,保险箱Pro、私人保险箱、私密相冊打开就闪退的官方解决方式
  10. linux系统安装配置exim4(源码安装)
  11. secureCRT常见命令
  12. JavaWeb学习 (十四)————JSP基础语法
  13. TLAB
  14. python类脚本
  15. Linux运维之批量下载指定网站的100个图片文件,并找出大于200KB的文件
  16. C++ 在容器A中查找最后出现的容器B中的元素,并返回iterator(find_end)
  17. 加密算法(DES,AES,RSA,MD5,SHA1,Base64)比较和项目应用
  18. printf()的转换说明的修饰符中的标记、数字、和.数字
  19. KM算法(最优匹配)
  20. spring boot web服务

热门文章

  1. jQuery的基本用法:
  2. 根据序列图像聚焦区域获取深度 Shape From Focus
  3. callback res.end 记得return(Javascript需要养成的良好习惯)
  4. java中Jbutton常用设置
  5. 黑马程序员——C语言基础 指针
  6. 阿伦学习html5 之 Local Storage (本地储存)
  7. web.config 配置
  8. python获取指定时间段内的随机不重复的时间点
  9. Activity的保存状态和状态恢复
  10. android.os.NetworkOnMainThreadException异常