Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

用開源庫 Xutils 實現 Android 多線程文件下載

編輯:關於 Android 程式設計

自己手寫下載邏輯往往要處理線程、流、斷點、進度回調等一堆細節,稍不留神就容易出錯。其實借助成熟開源庫 Xutils,幾行代碼就能實現帶進度回調的文件下載,開發效率大大提升。

一、布局文件中搭建界面

先在一個垂直的 LinearLayout 中放置輸入框、下載按鈕與提示文本:

<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    xmlns:android="https://schemas.android.com/apk/res/android"
    xmlns:tools="https://schemas.android.com/tools"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/tv_path"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:text="https://down.360safe.com/inst.exe" />

    <Button
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_width="match_parent"
        android:onClick="download"
        android:text="下載" />

    <TextView
        android:id="@+id/tv_info"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dip"
        android:layout_width="match_parent"
        android:text="提示信息" />
</LinearLayout>

二、在 MainActivity 中實現下載邏輯

核心是利用 HttpUtils.download() 並傳入一個 RequestCallBack,在其中處理開始、加載中、失敗與成功四種回調:

package com.wzw.downloaddemo;

import java.io.File;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.callback.RequestCallBack;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity {

    private EditText etPath;
    private TextView tvInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etPath = (EditText) findViewById(R.id.tv_path);
        tvInfo = (TextView) findViewById(R.id.tv_info);
    }

    public void download(View v) {
        String path = etPath.getText().toString();
        HttpUtils http = new HttpUtils();
        http.download(path, "/sdcard/360.exe", true, true, new RequestCallBack() {
            @Override
            public void onStart() {
                tvInfo.setText("正在連接...");
            }

            @Override
            public void onLoading(long total, long current, boolean isUploading) {
                tvInfo.setText(current + " / " + total);
            }

            @Override
            public void onFailure(HttpException error, String msg) {
                tvInfo.setText(msg);
            }

            @Override
            public void onSuccess(ResponseInfo responseInfo) {
                tvInfo.setText("downloaded: " + responseInfo.result.getPath());
            }
        });
    }
}

可以看到,相比傳統的「手動開線程 + 讀寫流 + 計算進度」方式,Xutils 把下載、斷點續傳、進度回調都封裝得妥妥當當,我們只需關注業務回調即可。在 onLoading 中實時刷新已下載與總大小,就能輕鬆呈現下載進度。這種「開箱即用」的第三方庫,正是提升開發效率的一大法寶。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved