-1

I've gotten my app to download content from a website but the intent asks you to open the link in the browser and downloads it from there. I wish to change that so when i tap the download button, instead of the intent directing to the browser it opens a popup in my app and shows the download progress.

How am I able to achieve this? I have searched around stackoverflow and have only found ways that don't work for me. Here is my code -

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();

        }
    }
});

Could someone explain what is the best way to create a popup download progress? Because i have an input field where one can enter the second part of the url and it downloads that. Eg- download.com/ but the user enters "download" so when he taps the button it changes to download.com/download and it downloads from that url.

Thanks

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
LoopyLoo
  • 1
  • 4
  • 1. you can always use DownloadManager(especially for large files) 2. if not then any http client (Apache, URLConnection, etc.) 3. for each http clients there is pleanty examples how to do this over the internet ... so in other words: do more reaserch – Selvin Feb 12 '15 at 12:05
  • You should create a `AlertDialog` in a new `Activity` and open that activity `onClick` of that download link and don't forget to finish these few lines on Positive button of `AlertDialog` `Intent i = new Intent( Intent.ACTION_VIEW) .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); i.setData(Uri.parse(url_link)); startActivity(i); finish();` – Anshul Tyagi Feb 12 '15 at 12:05
  • Thanks for the response, I'll do more research into this. – LoopyLoo Feb 12 '15 at 12:08

1 Answers1

0

You can do something like this..

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;
  }
}

Then write an async task to update progress..

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);
 }
}

Write your download logic under the doInBackground method of Asynctask.This should solve your purpose and it wont even block UI tread..

King of Masses
  • 18,405
  • 4
  • 60
  • 77