Android之ProgressBar初步應用

這裏利用 ProgressBar 即時顯示下載進度。java

途中碰到的問題:android

一、主線程中不能打開 URL,和只能在主線程中使用 Toast 等網絡

二、子線程不能修改 UIapp

三、容許網絡協議ide

四、暫停下載和繼續下載佈局

  ........this

 

 

fragment_main 佈局文件url

 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.xmlspa

 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 文件中配置.net

1 <!-- 請求網絡權限 -->
2     <uses-permission  android:name="android.permission.INTERNET"/>

MainActivity(問題一、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資源.....***************

相關文章
相關標籤/搜索