Android 下载功能实现:添加下载进度显示

本教程将向您展示如何在 Android 应用中实现下载功能,并添加下载进度显示功能。

1. 布局文件添加进度显示

首先,在布局文件 activity_main.xml 中添加一个 TextView 来显示下载进度:

<TextView
    android:id="@+id/download_progress"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="下载进度:0%"
    android:textSize="18sp"
    android:layout_marginTop="16dp"
    android:layout_marginBottom="16dp"
    android:visibility="gone" />

2. MainActivity 添加进度更新方法

MainActivity.java 中添加一个 TextView 的成员变量和一个更新下载进度的方法:

private TextView downloadProgress;

private void updateDownloadProgress(int progress) {
    downloadProgress.setText('下载进度:' + progress + '%');
}

3. 初始化进度显示并更新进度

onCreate 方法中初始化 downloadProgress,并在 onClick 方法中调用 updateDownloadProgress 方法更新下载进度:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // ...
    downloadProgress = findViewById(R.id.download_progress);
    // ...
}

@Override
public void onClick(View v) {
    // ...
    if (id == R.id.start_download) {
        // ...
        downloadProgress.setVisibility(View.VISIBLE); // 显示下载进度
    } else if (id == R.id.cancel_download) {
        // ...
        downloadProgress.setVisibility(View.GONE); // 隐藏下载进度
    }
    // ...
}

4. DownloadService 添加进度更新方法

最后,在 DownloadService.java 中添加一个更新下载进度的方法,并在下载过程中调用该方法更新进度:

private DownloadTask downloadTask;

private class DownloadTask extends AsyncTask<String, Integer, Integer> {

    // ...

    @Override
    protected void onProgressUpdate(Integer... values) {
        int progress = values[0];
        if (progress > lastProgress) {
            // 更新下载进度
            downloadBinder.updateDownloadProgress(progress);
            lastProgress = progress;
        }
    }

    // ...
}

代码示例

// MainActivity.java
// ...
@Override
public void onClick(View v) {
    // ...
    if (id == R.id.start_download) {
        downloadBinder.startDownload(url);
        isDownload = true;
        startDownload.setEnabled(false);
        pauseDownload.setEnabled(true);
        cancelDownload.setEnabled(true);
        downloadProgress.setVisibility(View.VISIBLE); // 显示下载进度
    } else if (id == R.id.pause_download) {
        if (isDownload) {
            if (!isContinue) {
                downloadBinder.pauseDownload();
                pauseDownload.setText("继续下载");
                pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
                isContinue = true;
            } else {
                downloadBinder.startDownload(url);
                pauseDownload.setText("暂停下载");
                pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorGray));
                isContinue = false;
            }
        } else {
            Toast.makeText(this, "请先点击下载", Toast.LENGTH_SHORT).show();
        }
    } else if (id == R.id.cancel_download) {
        downloadBinder.cancelDownload();
        startDownload.setEnabled(true);
        pauseDownload.setEnabled(true);
        cancelDownload.setEnabled(false);
        pauseDownload.setBackgroundColor(getResources().getColor(R.color.colorGray));
        downloadProgress.setVisibility(View.GONE); // 隐藏下载进度
    }
    // ...
}
// ...

// DownloadService.java
// ...
private DownloadTask downloadTask;

private class DownloadTask extends AsyncTask<String, Integer, Integer> {

    // ...

    @Override
    protected void onProgressUpdate(Integer... values) {
        int progress = values[0];
        if (progress > lastProgress) {
            // 更新下载进度
            downloadBinder.updateDownloadProgress(progress);
            lastProgress = progress;
        }
    }

    // ...
}

通过以上步骤,您就可以在 Android 应用中实现下载功能,并在下载过程中显示下载进度了。

Android 下载功能实现:添加下载进度显示

原文地址: https://www.cveoy.top/t/topic/Nf1 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录