Hi gals and guys!

I've came across many unsolved issues regarding WebView with support for 
downloading files in Android.

Here's the code (whole MainActivity.java) for simple WebView:

package com.webview.downloader;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.pad.android.xappad.AdController;

public class MainActivity extends Activity {

    WebView mWeb;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        mWeb = new WebView(this);
        setContentView(mWeb);
        WebSettings settings = mWeb.getSettings();
        settings.setJavaScriptEnabled(true);

        mWeb.setWebViewClient(new WebViewClient() {
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                    view.loadUrl(url);
                    return true;
            }
        });
        mWeb.loadUrl("http://www.site-with-direct-links.somehostingsite.com";);
    }
}

With this code, after opening URL and upon clicking on the link, nothing 
happens.

We need to add the following code (just right after mWeb.loadUrl();):

  mWeb.setDownloadListener(new DownloadListener() {
      public void onDownloadStart(String url, String userAgent,
              String contentDisposition, String mimetype,
              long contentLength) {
        Intent i = new Intent(Intent.ACTION_VIEW);
        i.setData(Uri.parse(url));
        startActivity(i);
      }
  });

With this code, now we can surf web and also download files, ex. upon click 
on something like:*http://www.google.cz*, the WebView redirects user to 
that site, when clicked on:*http://www.google.cz/download-file.apk*, 
default browser or any other browser like Google Chrome (after choice) will 
popup for a short time and then Download will start.

So my question is following: how can I achieve to start downloading files (*
.zip*, *.rar*, *.apk*) without opening any browser.

Of course, I've looked into docs, also searched stack, but never had a luck 
with this. Real example would be good, changes to my code sounds even 
better!

Thanks for any ideas!

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to