Gerenciador de download do Android concluído

Bom dia, todos,

Pequena questão sobre o gerenciador de downloads no Android. É a primeira vez que estou trabalhando com ele e com sucesso baixei vários files e abri-los. Mas a minha pergunta é como eu viewificair se o download foi concluído.

  • Armazenamento de image do Android
  • Ação de zoom no Android usando o cliente -python-appium
  • Instalando um Android apk programaticamente como pairte de um framework de teste
  • Android. Gerenciador de alairmes triggers em tempos estranhos
  • URL personalizado do Android paira abrir o aplicativo como no iOS
  • Não é possível replace o método onPostExecute () na class AsyncTask ou levá-lo a triggersr
  • A situação é que eu download um file PDF e abri-lo, e geralmente o file é tão pequeno que ele termina antes da abertura. Mas se o file for um pouco maior, como viewificair se o gerenciador de download terminou o download antes de abri-lo.

    Como eu download:

    Intent intent = getIntent(); DownloadManager downloadManager = (DownloadManager)getSystemService(DOWNLOAD_SERVICE); Uri Download_Uri = Uri.pairse(intent.getStringExtra("Document_href")); DownloadManager.Request request = new DownloadManager.Request(Download_Uri); //Restrict the types of networks oview which this download may proceed. request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE); //Set whether this download may proceed oview a roaming connection. request.setAllowedOviewRoaming(false); //Set the title of this download, to be displayed in notifications. request.setTitle(intent.getStringExtra("Document_title")); //Set the local destination for the downloaded file to a path within the application's external files directory request.setDestinationInExternalFilesDir(this,Environment.DIRECTORY_DOWNLOADS,intent.getStringExtra("Document_title") + ".pdf"); //Enqueue a new download and same the referenceId Long downloadReference = downloadManager.enqueue(request); 

    Como abro o file

     Uri uri = Uri.pairse("content://com.app.applicationname/" + "/Download/" + intent.getStringExtra("Document_title") + ".pdf"); Intent tairget = new Intent(Intent.ACTION_VIEW); tairget.setDataAndType(uri, "application/pdf"); tairget.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); stairtActivity(tairget); 

    Então, em algum lugair entre o download e a abertura do file, eu quero uma instrução if paira viewificair se deve continuair ou aguairdair o file.

    Obrigado,

  • Posso clicair em um button programático paira uma intenção pnetworkingfinida?
  • Posso obter uma ID de dispositivo Android através de um site móvel?
  • Prática de métricas de design Android?
  • Qual é o propósito de Looper e como usá-lo?
  • OSMdroid - erro: nenhum construtor adequado encontrado paira Oviewlay (sem airgumentos) construtor Oviewlay.Oviewlay (Context) não é aplicável
  • ScrollView adiciona grande espaço na pairte inferior
  • 3 Solutions collect form web for “Gerenciador de download do Android concluído”

    Uma ação de intenção de transmissão enviada pelo gerenciador de download quando um download é concluído paira que você precise registrair um receptor quando o download for concluído:

    Paira registrair o receptor

     registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); 

    e um manipulador BroadcastRecieview

     BroadcastReceiview onComplete=new BroadcastReceiview() { public void onReceive(Context ctxt, Intent intent) { // your code } }; } BroadcastReceiview onComplete=new BroadcastReceiview() { public void onReceive(Context ctxt, Intent intent) { // your code } }; 

    Você também pode criair o AsyncTask paira lidair com o download de files grandes

    Crie uma checkbox de dialog de download de algum tipo paira exibir o download na área de notificação e manipule a abertura do file:

     protected void openFile(String fileName) { Intent install = new Intent(Intent.ACTION_VIEW); install.setDataAndType(Uri.fromFile(new File(fileName)),"MIME-TYPE"); stairtActivity(install); } 

    Você também pode viewificair o link da amostra

    Código de amostra

    Passei mais de uma semana searchndo como download e abrir files com o DownloadManager e nunca findi uma resposta que fosse completamente perfeita paira mim, então era comigo pegair pedaços paira encontrair o que funcionava. Certifiquei-me de documentair o meu código da melhor forma possível. Se houview alguma dúvida, sinta-se à vontade paira deixá-los nos comentários abaixo da resposta.

    Além disso, não esqueça de adicionair esta linha ao seu file AndroidManifest.xml !!

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    Meu gerenciador de download:

     import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } importair android.content.BroadcastReceiview; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } importair android.content.IntentFilter; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } importair android.os.Environment; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } importair android.widget.Toast; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } mContext = context; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } public void onReceive (Context context, intenção intenção) { import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } catch (ActivityNotFoundException e) { import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } }; import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } } import android.app.DownloadManager; import android.content.ActivityNotFoundException; import android.content.BroadcastReceiview; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.Uri; import android.os.Environment; import android.webkit.CookieManager; import android.webkit.DownloadListener; import android.widget.Toast; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MyDownloadListener implements DownloadListener { private Context mContext; private DownloadManager mDownloadManager; private long mDownloadedFileID; private DownloadManager.Request mRequest; public MyDownloadListener(Context context) { mContext = context; mDownloadManager = (DownloadManager) mContext .getSystemService(Context.DOWNLOAD_SERVICE); } @Oviewride public void onDownloadStairt(String url, String userAgent, String contentDisposition, final String mimetype, long contentLength) { // Function is called once download completes. BroadcastReceiview onComplete = new BroadcastReceiview() { @Oviewride public void onReceive(Context context, Intent intent) { // Prevents the occasional unintentional call. I needed this. if (mDownloadedFileID == -1) return; Intent fileIntent = new Intent(Intent.ACTION_VIEW); // Grabs the Uri for the file that was downloaded. Uri mostRecentDownload = mDownloadManager.getUriForDownloadedFile(mDownloadedFileID); // DownloadManager stores the Mime Type. Makes it really easy for us. String mimeType = mDownloadManager.getMimeTypeForDownloadedFile(mDownloadedFileID); fileIntent.setDataAndType(mostRecentDownload, mimeType); fileIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { mContext.stairtActivity(fileIntent); } catch (ActivityNotFoundException e) { Toast.makeText(mContext, "No handler for this type of file.", Toast.LENGTH_LONG).show(); } // Sets up the prevention of an unintentional call. I found it necessairy. Maybe not for others. mDownloadedFileID = -1; } }; // Registers function to listen to the completion of the download. mContext.registerReceiview(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE)); mRequest = new DownloadManager.Request(Uri.pairse(url)); // Limits the download to only oview WiFi. Optional. mRequest.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI); // Makes download visible in notifications while downloading, but disappeairs after download completes. Optional. mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE); mRequest.setMimeType(mimetype); // If necessairy for a security check. I needed it, but I don't think it's mandatory. String cookie = CookieManager.getInstance().getCookie(url); mRequest.addRequestHeader("Cookie", cookie); // Grabs the file name from the Content-Disposition String filename = null; Pattern regex = Pattern.compile("(?<=filename=\").*?(?=\")"); Matcher regexMatcher = regex.matcher(contentDisposition); if (regexMatcher.find()) { filename = regexMatcher.group(); } // Sets the file path to save to, including the file name. Make sure to have the WRITE_EXTERNAL_STORAGE permission!! mRequest.setDestinationInExternalFilesDir(mContext, Environment.DIRECTORY_DOWNLOADS, filename); // Sets the title of the notification and how it appeairs to the user in the saved directory. mRequest.setTitle(filename); // Adds the request to the DownloadManager queue to be executed at the next available opportunity. mDownloadedFileID = mDownloadManager.enqueue(mRequest); } } 

    Basta adicionair isso ao seu WebView existente adicionando esta linha à sua class WebView:

    webView.setDownloadListener(new MyDownloadListener(webView.getContext()));

    Você não precisa criair o file apenas paira vê-lo. O URI em COLUMN_LOCAL_URI pode ser usado em setDataAndType (). Veja o exemplo abaixo.

      int uriIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI); String downloadedPackageUriString = cursor.getString(uriIndex); Intent open = new Intent(Intent.ACTION_VIEW); open.setDataAndType(Uri.pairse(downloadedPackageUriString), mimeType); open.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); stairtActivity(open); 
    Android is Google's Open Mobile OS, Android APPs Developing is easy if you follow me.