Android之ProgressBar即時顯示下載進(jìn)度詳解
這里利用 ProgressBar 即時顯示下載進(jìn)度。
途中碰到的問題:
1、主線程中不能打開 URL,和只能在主線程中使用 Toast 等
2、子線程不能修改 UI
3、允許網(wǎng)絡(luò)協(xié)議
4、暫停下載和繼續(xù)下載
........
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 進(jìn)度條 --> <!-- progress 當(dāng)前進(jìn)度 --> <!-- indeterminate 不明確的 默認(rèn)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>
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">繼續(xù)</string> </resources>
(問題3)在 AndroidManifest 文件中配置
<!-- 請求網(wǎng)絡(luò)權(quán)限 -->
<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; /** * 只有創(chuàng)建一個 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(); // 設(shè)置按鈕內(nèi)容 ----并沒有用... 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; // 注意強(qiáng)轉(zhuǎn)方式,防止一直為0 percent = (int) (100.0 * sum / contentLength); // 在主線程上運(yùn)行的子線程 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,要用斷點(diǎn)續(xù)傳,但是還不會存放assets資源.....***************
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 解析android中ProgressBar的用法
- android中ProgressDialog與ProgressBar的使用詳解
- android ListView和ProgressBar(進(jìn)度條控件)的使用方法
- Android編程之ProgressBar圓形進(jìn)度條顏色設(shè)置方法
- Android三種方式實(shí)現(xiàn)ProgressBar自定義圓形進(jìn)度條
- Android ProgressBar進(jìn)度條使用詳解
- Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
- Android編程實(shí)現(xiàn)自定義ProgressBar樣式示例(背景色及一級、二級進(jìn)度條顏色)
- Android ProgressBar直線進(jìn)度條的實(shí)例代碼
- Android progressbar實(shí)現(xiàn)帶底部指示器和文字的進(jìn)度條
相關(guān)文章
Android Native 內(nèi)存泄漏系統(tǒng)化解決方案
這篇文章主要介紹了Android Native 內(nèi)存泄漏系統(tǒng)化解決方案,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07Flutter 使用fluro的轉(zhuǎn)場動畫進(jìn)行頁面切換
在實(shí)際應(yīng)用中,我們常常會對不同的頁面采取不同的轉(zhuǎn)場動畫,以提高頁面切換過程中的用戶體驗(yàn)。例如,微信的掃碼后在手機(jī)上確認(rèn)登錄頁面就是從底部彈出的,而大部分頁面的跳轉(zhuǎn)都是從右向左滑入。通過這種形式區(qū)分不同的轉(zhuǎn)場場景,從而給用戶更多的趣味性以提高用戶體驗(yàn)。2021-06-06Android 7.0 手電筒控制實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Android 7.0 手電筒控制實(shí)現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-09-09Android中實(shí)現(xiàn)圖文并茂的按鈕實(shí)例代碼
這篇文章主要介紹了Android中實(shí)現(xiàn)圖文并茂的按鈕實(shí)例代碼,代碼簡單易懂,非常不錯,需要的朋友可以參考下2017-04-04基于Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問題詳解
本篇文章對Android中Webview使用自定義的javascript進(jìn)行回調(diào)的問題進(jìn)行了詳細(xì)的分析介紹。需要的朋友參考下2013-05-05深入Android HandlerThread 使用及其源碼完全解析
這篇文章主要介紹了深入Android HandlerThread 使用及其源碼完全解析,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-08-08Android中使用TextView實(shí)現(xiàn)高仿京東淘寶各種倒計時效果
今天給大家?guī)淼氖莾H僅使用一個TextView實(shí)現(xiàn)一個高仿京東、淘寶、唯品會等各種電商APP的活動倒計時。今天小編把實(shí)現(xiàn)代碼分享到腳本之家平臺,對android textclock 倒計時效果感興趣的朋友參考下吧2016-10-10