最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Android之ProgressBar即時顯示下載進度詳解

 更新時間:2016年09月02日 14:58:49   作者:西門吃雪  
這篇文章主要為大家詳細介紹了Android之ProgressBar即時顯示下載進度,具有一定的參考價值,感興趣的小伙伴們可以參考一下

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

途中碰到的問題: 

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 進度條 -->
  <!-- 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>

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;
              // 注意強轉(zhuǎn)方式,防止一直為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,要用斷點續(xù)傳,但是還不會存放assets資源.....***************

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • Android添加聯(lián)系人到通訊錄的方法

    Android添加聯(lián)系人到通訊錄的方法

    本周項目中遇到了需要添加聯(lián)系人或者添加到已有聯(lián)系人的需求,聯(lián)系人中需要保存的字段有很多,之前不太熟悉,在這里總結(jié)一下。
    2021-05-05
  • Android使用AndroidUtilCode實現(xiàn)多語言

    Android使用AndroidUtilCode實現(xiàn)多語言

    這篇文章主要為大家介紹了Android使用AndroidUtilCode實現(xiàn)多語言示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
    2023-01-01
  • android自定義ImageView仿圖片上傳示例

    android自定義ImageView仿圖片上傳示例

    本篇文章主要介紹了android自定義ImageView仿圖片上傳,具有一定的參考價值,有興趣的可以了解一下。
    2017-01-01
  • Android延遲實現(xiàn)的幾種解決方法及原理分析

    Android延遲實現(xiàn)的幾種解決方法及原理分析

    這篇文章主要給大家介紹了關(guān)于Android延遲實現(xiàn)的幾種解決方法以及其中的原理分析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • Android實現(xiàn)幀動畫的兩種方式

    Android實現(xiàn)幀動畫的兩種方式

    幀動畫(Frame?Animation)是一種在一定時間內(nèi)按順序播放一系列圖像幀(每一幀都是一個單獨的圖像),從而產(chǎn)生連續(xù)運動或變化的動畫效果,本文給大家介紹了Android實現(xiàn)幀動畫的兩種方式,需要的朋友可以參考下
    2024-02-02
  • Android ViewPager自定義輪播圖并解決播放沖突

    Android ViewPager自定義輪播圖并解決播放沖突

    這篇文章主要為大家詳細介紹了Android ViewPager自定義輪播圖并解決播放沖突,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-09-09
  • Android Studio ADB網(wǎng)絡(luò)調(diào)試匯總

    Android Studio ADB網(wǎng)絡(luò)調(diào)試匯總

    這篇文章主要為大家詳細介紹了Android Studio ADB網(wǎng)絡(luò)調(diào)試的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • 深入分析Android加載so文件源碼

    深入分析Android加載so文件源碼

    這篇文章主要介紹了深入分析Android加載so文件源碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-03-03
  • Android實現(xiàn)進度條(ProgressBar)的功能與用法

    Android實現(xiàn)進度條(ProgressBar)的功能與用法

    這篇文章主要為大家詳細介紹了Android實現(xiàn)進度條(ProgressBar)的功能與用法,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-08-08
  • 自定義視圖view之環(huán)形進度條

    自定義視圖view之環(huán)形進度條

    這篇文章主要介紹了自定義視圖view之環(huán)形進度條,這次介紹了4種不同的效果,直接上代碼了,需要的朋友可以參考下
    2023-04-04

最新評論

通渭县| 建水县| 天等县| 绥化市| 新竹县| 伊金霍洛旗| 福建省| 青岛市| 宁化县| 墨玉县| 西城区| 商南县| 纳雍县| 长丰县| 天峻县| 邻水| 龙岩市| 张北县| 子洲县| 隆回县| 安宁市| 揭阳市| 台中县| 泰来县| 延寿县| 安乡县| 上犹县| 贞丰县| 平度市| 垫江县| 根河市| 英山县| 玉溪市| 理塘县| 呼伦贝尔市| 瓮安县| 浙江省| 凌云县| 星子县| 拉萨市| 紫阳县|