如何在应用程序中显示下载进度

时间:2015-02-12 12:02:28

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

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

我怎么能做到这一点? 我搜索了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();

        }
    }
});

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

谢谢

1 个答案:

答案 0 :(得分:0)

你可以这样做......

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方法下编写你的下载逻辑。这应该解决你的目的,它甚至不会阻止用户界面...