这里利用 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资源.....***************

 

Android之ProgressBar初步应用的更多相关文章

  1. java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.ProgressBar$SavedState

    java.lang.ClassCastException: android.view.AbsSavedState$1 cannot be cast to android.widget.Progress ...

  2. Android之ProgressBar

    今天复习一下以前的知识,补充一下ProgressBar控件 progressBar是进度条组件,通常用于用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好性. ...

  3. Android自定义progressBar

    通过继承系统ProgressBar实现 效果图 实现 HorizontalProgressBarWithNumber 自定义属性 <?xml version="1.0" en ...

  4. Android的ProgressBar以及自定义进度条

    1.xml文件 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/an ...

  5. Android的ProgressBar

    注意点: 必须在setContentView 前面设置,否则会报错. 重要的方法: progress.incrementProgressBy(int diff);//参数为进度数,进度满了为100.不 ...

  6. Android的ProgressBar进度条-android学习之旅(三十一)

    ProgressBar 简介 ProgressBar是一种很常用的Ui,用于给复杂的操作显示进度,提供更好的用户相应.使用setProgress()incrementProgressBy()来设置进度 ...

  7. Android自定义ProgressBar样式

    我们使用的进度条多种多样,下面有几种自定义的进度条的样式,下面介绍几个. 进度条的有基本的四种样式: 默认风格的进度条: android:progressBarStyle 水平长型进度条: andro ...

  8. Android 使用ProgressBar实现进度条

    ProgressBar简介ProgressBar是进度条组件,通常用于向用户展示某个耗时操作完成的进度,而不让用户感觉是程序失去了响应,从而更好地提升用户界面的友好型. 课程目标(1)制定Progre ...

  9. android 自定义progressbar 样式

    在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://sche ...

随机推荐

  1. 【编辑器】【Sublime Text】使用笔记

    1.安装 官网下载即可 2.插件 sublime-text - Sublime Text 怎么高亮 Markdown 的文件语法 设置Sublime为VIM模式 如何在sublime 里面设置 ver ...

  2. 数据表格datagrid内容整理

    <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding= ...

  3. 一个Woker类,当id和name相同时,系统判断两个工人是相等的,打印工人对象时显示“工人:id和name”。

    public class Worker { private int id; private String name; private double salary; public boolean equ ...

  4. 不一样的Android选择器,简单方便,地址日期时间都好用!

    前言 Android开发有不少情况下会用到Picker,例如选择 地址.日期.时间. 原生Picker和仿iOS的Picker都是上下或左右滑动到固定区域来选择选项: 显示数量少,如果当前选项距离需要 ...

  5. low-rank 的相关求解方法 (CODE) Low-Rank Matrix Recovery and Completion via Convex Optimization

    (CODE) Low-Rank Matrix Recovery and Completion via Convex Optimization 这个是来自http://blog.sina.com.cn/ ...

  6. hdu 1082, stack emulation, and how to remove redundancy 分类: hdoj 2015-07-16 02:24 86人阅读 评论(0) 收藏

    use fgets, and remove the potential '\n' in the string's last postion. (main point) remove redundanc ...

  7. Extjs4 -- Ext.loader命名空间的配置

    初次使用extjs4的版本,在配置学习Ext.Loader()进行js文件的动态加载机制,由于各种原因导致多次失败,纠结2天,现将解决时出现的问题及需要注意事项进行记录 开发环境myeclipse8. ...

  8. leetcode日记 Product of Array Except Self

    Given an array of n integers where n > 1, nums, return an array output such that output[i] is equ ...

  9. php序列化和反序列化

    一 使用系统函数serialize和unserilazie <?php class A { public $a = "aa"; public $b = 10; functio ...

  10. Android Service提高

    我们从以下几个方面来了解Service IntentService的使用 Service与Thread的区别 Service生命周期 前台服务 服务资源被系统以外回收处理办法 不被销毁的服务 Inte ...