Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8439581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T08:02:02+00:00 2026-06-10T08:02:02+00:00

I have this little code here when click a link on webview that is

  • 0

I have this little code here when click a link on webview that is a link to a file, in this case .mp4. this code will go to default web browser and request app that can view this file type.

myWebView.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);
    }
});

What i want is when i click that file link, it create a dialog that ask weather to download or view the file. If click download, i want to use DownloadManager class to handle it and download that file in the background and alert when completed. And if click view, i want to create intent that ask for app that can view this file without going to the web browser.

    private class MyWebViewClient extends WebViewClient {

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, final String url) {

        if (url.endsWith(".mp4")) {
            AlertDialog.Builder builder = new AlertDialog.Builder(context);
            builder.setMessage(R.string.dialog_title)
                .setCancelable(false)
                .setPositiveButton(R.string.dialog_download, new DialogInterface.OnClickListener() {                        

                    public void onClick(DialogInterface dialog, int id) {
                                DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
                                request.setDescription("Some descrition");
                                request.setTitle("Some title");
                                // in order for this if to run, you must use the android 3.2 to compile your app
                                request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "name-of-the-file.ext");

                                // get download service and enqueue file
                                DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
                                manager.enqueue(request);
                    }
                })
                .setNegativeButton(R.string.dialog_play, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface dialog, int id) {
                        WebView myWebView = (WebView) findViewById(R.id.webview);                           
                        myWebView.setDownloadListener(new DownloadListener() {
                            public void onDownloadStart(String url, String userAgent,
                                    String contentDisposition, String mimetype,
                                    long contentLength) {
                                Intent intent = new Intent();
                                intent.setAction(Intent.ACTION_VIEW);
                                intent.setDataAndType(Uri.parse(url), "video/mp4");
                                startActivity(intent);
                            }
                        });

                    }
                });
            AlertDialog alert = builder.create();
            alert.show();
        }
        return false;
    }
}

Now i got this code that prompt user to download or play mp4 file that the user clicked. But when i click Play or Download it is work until i click the link the second time, what’s wrong with the code above if anyone can correct this please. thanks.

I’m very new to Android developing and java, if anyone could guide me through this it will help me learn faster. And also sorry for my English…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T08:02:05+00:00Added an answer on June 10, 2026 at 8:02 am

    The solution to your problem relies on intercepting the URL that your webview is trying to load. Create a WebViewClient and overwrite the shouldOverrideUrlLoading method:

    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.endsWith(".mp3")) { //or whatever other extension
          //Prompt user for action (save or view)
        }
        return false;
     }
    

    Then, depending on what the user chooses, start an AsyncTask for downloading to handle downloading the file, or launch the intent for viewing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this little piece of code that just checks for a particular text
I have taken this little snippet straight out of some code I'm working on:
its a little bit hard to understand. in the header.php i have this code:
I have a little problem. This is my code: stackPanelShapesContainer = new StackPanel(); InitializeStackPanle();
I have this little problem, that I cannot figure out which arguments to pass
I have this little question that's been on my mind for a while now.
I have this little problem. My client wanted two distinct swf on a web
I have this little script that shows one wisdom each day. so I have
I have a little personal webapp that I'm working on. I have a link
I have a link ('#login_display') that slides down a sub-menu '('.box_login')` on click. What

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.