Android之ProgressBar初步应用

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

途中碰到的问题:

1、主线程中不能打开 URL、使用 Toast 等

2、子线程不能修改 UI

3、允许网络协议

4、暂停下载和继续下载

  ........

fragment_main 布局文件

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent"
 5     tools:context="com.dragon.android.textbar.MainActivity$PlaceholderFragment" >
 6
 7     <!-- prigressBar 进度条 -->
 8     <!-- progress 当前进度 -->
 9     <!-- indeterminate 不明确的   默认false -->
10     <ProgressBar
11         android:id="@+id/progressBar1"
12         style="?android:attr/progressBarStyleHorizontal"
13         android:layout_width="match_parent"
14         android:layout_height="wrap_content"
15         android:layout_centerInParent="true"
16         android:max="100"
17         android:progress="0"
18         android:indeterminate="true"/>
19
20     <Button
21         android:id="@+id/button1"
22         android:layout_width="wrap_content"
23         android:layout_height="wrap_content"
24         android:layout_alignParentTop="true"
25         android:layout_centerHorizontal="true"
26         android:onClick="startLoad"
27         android:layout_marginTop="86dp"
28         android:background="#009FEE"
29         android:text="@string/start"
30         android:textColor="#ffffff" />
31
32     <TextView
33         android:id="@+id/textView1"
34         android:layout_width="wrap_content"
35         android:layout_height="wrap_content"
36         android:layout_above="@+id/progressBar1"
37         android:background="@null"
38         android:layout_alignParentLeft="true" />
39
40 </RelativeLayout>

fragment_main

strings.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <resources>
 3
 4     <string name="app_name">hwdownload</string>
 5     <string name="hello_world">Hello world!</string>
 6     <string name="action_settings">Settings</string>
 7     <string name="start">开始</string>
 8     <string name="stop">暂停</string>
 9     <string name="contin">继续</string>
10
11 </resources>

strings

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

1 <!-- 请求网络权限 -->
2     <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资源.....***************

 

时间: 2024-08-24 06:47:20

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

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

ProgressBar 简介 ProgressBar是一种很常用的Ui,用于给复杂的操作显示进度,提供更好的用户相应.使用setProgress()incrementProgressBy()来设置进度和显示进度的增加或减少,正数表示增加,负数表示减少. ProgressBar的风格 ProgressBar 代码示例 布局代码 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns

android 自定义progressbar 样式

在res下创建drawable文件夹,新建文件drawable/progressbar_color.xml <layer-list xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 背景 gradient是

[Android UI] ProgressBar自定义

转载自:http://gundumw100.iteye.com/blog/1289348 1: 在JAVA代码中 在java代码中 ProgressBar      继承自View, 在android.widegt包中 ProgressDialog   继承自Dialog, 在 android.app包中. 2: 在XML文件中 ProgressBar 默认是圆形转圈.当为ProgressBar设置style="?android:attr/progressBarStyleHorizontal&q

从头学Android之Service初步二

在上一篇,我们学习了通过startService来启动Service,由于篇幅过长,所以这一篇是接上一篇的 二.bindService方法启动Service 先看bindSerivce(Intent service,ServiceConnection conn,int flags)函数 参数说明: service:通过该参数也就是Intent我们可以启动指定的Service conn:该参数是一个ServiceConnection对象,这个对角用于监听访问者(也可以说成是客户端)与Service

Android的ProgressBar

注意点: 必须在setContentView 前面设置,否则会报错. 重要的方法: progress.incrementProgressBy(int diff);//参数为进度数,进度满了为100.不能够超过100. progress.incrementSecondaryProgressBy(-10); package com.wyl.progressbartest; import android.app.Activity; import android.os.Bundle; import an

android用ProgressBar实现百分比的显示

显示水平进度条我想到的有两种:seekBar和ProgressBar,这两种都可以显示进度,最明显的区别是seekbar是可以用手拖动的,比如,应用程序中用户可以对音效进行控制,对音乐的播放进度进行控制,等等,都可以使用拖动条来实现 需要实现一个投票百分比的进度条: progressbar布局: 其中background指的是进度条背景是灰色的背景 style中是progressbar的一些属性 <style name="StyleProgressBar" parent=&quo

android学习ProgressBar的简单使用

android 提供的ProgressBar控件分为两种,一种是不带进度的进度条,一种是带进度的进度条,如果可以计算任务的完成量那么就用带进度条的,如果无法计算任务量,那么就使用不带进度的进度条.ProgressBar如果说只使用系统提供的,那就很简单,就只有那些属性方法,但是感觉比较难得就是ProgressBar的样式,一般做应用都不会直接使用系统提供的,而是在它的额基础上做进一步的美化. ProgressBar的关键属性 android:max 最大值 android:proress 第一进

Android之ProgressBar读取文件进度解析

ProgressBar进度条, 分为旋转进度条和水平进度条,进度条的样式根据需要自定义,之前一直不明白进度条如何在实际项目中使用,网上演示进度条的案例大多都是通过Button点 击增加.减少进度值,使用方法incrementProgressBy(int),最简单的做法是在xml布局文件中放置ProgressBar空间,然 后再MainActivity中触发事件后执行incrementProgressBy(int),代码如下: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

Android学习笔记-Android应用程序初步认识

一直觉得自己的技术没有一门专长,似乎什么都会一点,但是却一点都不深入.决定学习Android的开发,说不出的理由,希望自己能够坚持下去. 其实之前已经搭建好了Android的开发环境eclipse+ADT+SDK,这里就不做具体介绍了,个人觉得还是非常有必要把这3个软件单独安装一下, 这样对开发工具能有个系统的认识.Eclipse是一个IDE,针对多门开发语言都能够使用,SDK是针对Android应用开发提供的一个框架,其中有开发 过程中使用到的包和一些集成的工具,ADT是安装在eclipse上