Downloadlistener not working

How should DownloadListener work? I guess I missed something. I have done the following:

  • Register DownloadListener in WebView.
  • Open a WebView with an HTML page containing a link (works).
  • If I click on the link, DownloadListener is not called.

Here is a short piece of code.

package rene.android.learnit; import android.app.*; import android.os.Bundle; import android.webkit.*; public class ShowWeb extends Activity implements DownloadListener { public static Lesson L; WebView WV; @Override public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.showweb); WV=(WebView)findViewById(R.id.showweb); WV.setDownloadListener(this); WV.loadUrl("http://android.rene-grothmann.de/courses.html"); } public void onDownloadStart (String url, String agent, String disposition, String mimetype, long size) { Main.log(url+" "+mimetype+" "+size); } } 

Logging work (I use this everywhere to test my program), but nothing is logged, so the callback is not called. What happens: The view tries to download the file and does not work, since zip files are not supported on my Android.

The link goes to the zip file. It's ordinary

 <a href=...>...</a> 

link.

I tried to figure out an alternative intent registration method for zip files. But the documentation is so scarce that I could not do it. If I must, is there an example?

Any ideas?

Thanks R.

+3
source share
3 answers

It seems DownloadListener really is not working. However, you can use the following trick:

 package rene.android.learnit; import android.app.*; import android.os.Bundle; import android.webkit.*; public class ShowWeb extends Activity { public static Lesson L; WebView WV; @Override public void onCreate (Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.showweb); WV=(WebView)findViewById(R.id.showweb); WV.setWebViewClient(new WebViewClient() { public void onLoadResource (WebView view, String url) { Main.log(url); if (url.endsWith(".zip")) { finish(); } else super.onLoadResource(view,url); } } ); WV.loadUrl("http://android.rene-grothmann.de/courses.html"); } } 

This will process all zip files.

0
source

The reason it doesn't work is because you put the onDownloadStart method in the wrong place.

To be able to load the links that you selected on the first loaded page in WebView , you need to create a private class that extends WebViewClient . In this class, you override shouldOverrideUrlLoading(WebView v, String url) , otherwise the built-in browser will open a new URL instead.

This example explains that: http://developer.android.com/guide/tutorials/views/hello-webview.html

Then in your onCreate() method set webview.setWebViewClient(new WebViewClient()) . To be able to fire a load event, let your WebViewClient (created from the example above) implement the DownloadListener and override the onDownloadStart method.

Example:

 private class WVClient extends WebViewClient implements DownloadListener { @Override public boolean shouldOverrideUrlLoading(WebView v, String u) { v.loadUrl(u); v.setDownloadListener(this); return true; } @Override public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) { Log.i(TAG, "Download: " + url); Log.i(TAG, "Length: " + contentLength); } } 
+2
source

Make sure that:

 <uses-permission android:name="android.permission.INTERNET"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {... 
0
source

All Articles