【问题标题】:How to show download progress in app如何在应用中显示下载进度
【发布时间】:2015-04-13 03:49:56
【问题描述】:

我已经让我的应用程序从网站下载内容,但意图要求您在浏览器中打开链接并从那里下载。我希望改变这一点,所以当我点击下载按钮时,它不会直接指向浏览器,而是会在我的应用中打开一个弹出窗口并显示下载进度。

我怎样才能做到这一点? 我搜索了stackoverflow,只找到了对我不起作用的方法。这是我的代码-

txtLink = (EditText) findViewById(R.id.input_package);
btnOpenLink = (ImageButton) findViewById(R.id.download);
btnOpenLink.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        String page = txtLink.getText().toString();
        if (!TextUtils.isEmpty(page)) {
            Uri uri = Uri.parse(defaultLink + page);

            // Success Toast
            LayoutInflater inflater = getLayoutInflater();
            // Inflate the Layout
            View layout = inflater.inflate(R.layout.custom_toast, null);
            TextView text = (TextView) layout.findViewById(R.id.textToShow);
            // Set the Text to show in TextView
            text.setText("Your download should start shortly");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();
            Intent intent = new Intent(Intent.ACTION_VIEW, uri);
            startActivity(intent);
        } else {

            // Fail Toast
            LayoutInflater inflater = getLayoutInflater();
            // Inflate the Layout
            View layout = inflater.inflate(R.layout.custom_toast, null);
            TextView text = (TextView) layout.findViewById(R.id.textToShow);
            // Set the Text to show in TextView
            text.setText("Please enter a package name");
            Toast toast = new Toast(getApplicationContext());
            toast.setGravity(Gravity.FILL_HORIZONTAL, 0, 0);
            toast.setDuration(Toast.LENGTH_LONG);
            toast.setView(layout);
            toast.show();

        }
    }
});

有人能解释一下创建弹出下载进度的最佳方法是什么吗? 因为我有一个输入字段,可以在其中输入 url 的第二部分并下载它。例如,download.com/ 但用户输入“下载”,因此当他点击按钮时,它会更改为 download.com/download 并从该网址下载。

谢谢

【问题讨论】:

  • 1.你总是可以使用 DownloadManager(特别是对于大文件) 2. 如果没有,那么任何 http 客户端(Apache、URLConnection 等) 3. 对于每个 http 客户端,都有很多示例如何通过 Internet 执行此操作......所以在其他话:做更多的研究
  • 您应该在一个新的Activity 中创建一个AlertDialog 并打开该下载链接的onClick 活动,不要忘记在AlertDialog 的积极按钮上完成这几行987654326@
  • 感谢您的回复,我会对此进行更多研究。

标签: java android android-intent download android-download-manager


【解决方案1】:

你可以做这样的事情..

public static final int DIALOG_DOWNLOAD_PROGRESS = 0;
private ProgressDialog mProgressDialog;

@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DIALOG_DOWNLOAD_PROGRESS:
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("waiting 5 minutes..");
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setCancelable(false);
    mProgressDialog.show();
    return mProgressDialog;
default:
return null;
  }
}

然后写一个异步任务来更新进度..

private class DownloadZipFileTask extends AsyncTask<String, String, String> {

@Override
protected void onPreExecute() {
    super.onPreExecute();
    showDialog(DIALOG_DOWNLOAD_PROGRESS);
}

@Override
protected String doInBackground(String... urls) {
    //Copy you logic to calculate progress and call
    publishProgress("" + progress);
}

protected void onProgressUpdate(String... progress) {        
mProgressDialog.setProgress(Integer.parseInt(progress[0]));
}

@Override
protected void onPostExecute(String result) {           
    dismissDialog(DIALOG_DOWNLOAD_PROGRESS);
 }
}

在 Asynctask 的 doInBackground 方法下编写您的下载逻辑。这应该可以解决您的目的,它甚至不会阻塞 UI 踏板..

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多