Shubham Mathur
Shubham Mathur

Reputation: 401

Android: Using Webview but no downloading

I am using webview to open a website inside android app, but when clicking on download files link its not starting download. how i do it ???

The site is of music downlaod but when clicking on mp3 file link nothing happens.

Code:

public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.main);

    // Let's display the progress in the activity title bar, like the
    // browser app does.
    getWindow().requestFeature(Window.FEATURE_PROGRESS);

    WebView webview = new WebView(this);
    setContentView(webview);
    webview.getSettings().setJavaScriptEnabled(true);

    final Activity activity = this;
    webview.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) {
            // Activities and WebViews measure progress with different scales.
            // The progress meter will automatically disappear when we reach 100%
            activity.setProgress(progress * 1000);
        }
    });

    webview.setWebViewClient(new WebViewClient() {

        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            //Users will be notified in case there's an error (i.e. no internet connection)
            Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
        }
    });
    //This will load the webpage that we want to see
    webview.loadUrl("http://www.xtramasti.com/");



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

Upvotes: 0

Views: 911

Answers (1)

Boris Mocialov
Boris Mocialov

Reputation: 3489

try using webview.setDownloadListener(new DownloadListener() { ... });

Source

From documentation:

Registers the interface to be used when content can not be handled by the rendering engine, and should be downloaded instead. This will replace the current handler.

DownloadListener interface onDownloadStart method:

Notify the host application that a file should be downloaded

Upvotes: 1

Related Questions