开发者

downloadlistener not working

How is the DownloadListener supposed to work? Probably I miss something. I did the following:

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

Here is a short portion of the 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);
    }
}

The logging works (I am using this everywhere to check my program), but nothing is logged, so the callback is not called. What happens is: The view tries to download the file, and fails, since zip files are not supported on开发者_运维技巧 my Android.

The link goes to a zip-file. It is a usual

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

link.

I tried to figure out the alternative method of registering an intent for zip files. But the documentation is so sparse, I could not do that. If I have to, is there an example?

Any ideas?

Thanks, R.


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

To be able to load links that you select on your first loaded page in your WebView you need to create a private class extending a WebViewClient. In this class you override shouldOverrideUrlLoading(WebView v, String url) otherwise the built-in browser will open the 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 trigger on a download event let your WebViewClient (that you created from the example above) implement 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);
    }
}


It seems the DownloadListener is indeed not working. However, one 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 allow to handle all zip files.


Ensure 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) {...


Try this, I 100% sure this will be work

  1. Create your own WebClient like this
public class WVClient extends WebViewClient implements DownloadListener {
    private static final String TAG = "WVClient";
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        view.loadUrl(request.getUrl().toString());
        view.setDownloadListener(this);
        return super.shouldOverrideUrlLoading(view, request);
    }
    @Override
    public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
        Log.d(TAG, "manageDownloadsAction: URL:" + url);
        Log.d(TAG, "manageDownloadsAction: UserAgent:" + userAgent);
        Log.d(TAG, "manageDownloadsAction: contentDisposition:" + contentDisposition);
        Log.d(TAG, "manageDownloadsAction: mimeType:" + mimetype);
        Log.d(TAG, "manageDownloadsAction: contentLength:" + contentLength);
    }
  1. Then set your WebClient.
mWebView.setWebViewClient(new WVClient());


after enable following option to WebSettings, things works smoothly.

WebSettings settings = webView.getSettings();
    settings.setLoadsImagesAutomatically(true);
    
    
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜