3

I wonder if it's possible to launch a web browser from my app, and when the user clicks a URL which follows a specific pattern, it returns that URL.

Example:

http://domain.top-level-domain/download-folder/file-to-download.its-extension

I wan't to do this because I'm making an app with an integrated downloading system, and I want the user to be able to select the file to download in an easy way (by just clicking the URL). The process is like this:

  1. You log in to your personal page on a website.
  2. You browse your personal files.
  3. You select a file which you want to download.
  4. The app will automatically download and store it for you when you click the URL to the file.

Step 4 is the step I'm currently working on, and cannot really solve.
With an external browser, the process would have looked something like this:

  1. You log in to your personal page on a website.
  2. You browse your personal files.
  3. You select a file which you want to download.
  4. You copy the URL.
  5. You paste it in the app.

The process might not look more complicated, but I want to do it anyways.

EDIT: I found that the download link on that page was a line of javascript which sent the file to the user. Is it possible to execute that line of code on the page and retrieve the file anyway?

meager
  • 209,754
  • 38
  • 307
  • 315
Daniel Kvist
  • 2,642
  • 5
  • 22
  • 48

2 Answers2

2

If you manage the WebPage with links, you can achieve this with Custom Url Intent https://developer.chrome.com/multidevice/android/intents

This feature it's dependent on browser so Chrome will do but maybe other browser don't... :(

Here you have some examples
Android Respond To URL in Intent
Launch custom android application from android browser

The last time i have tried was working on a 50/60% of browsers, so......

Community
  • 1
  • 1
Sulfkain
  • 4,789
  • 1
  • 17
  • 37
1

You can intercept any kind of URI in your WebView by using a custom WebViewClient:

webView.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (url.contains("your_pattern")) {
            // Your stuff...
            return true;
        }
        return false;
    }
});

From the official documentation:

public boolean shouldOverrideUrlLoading (WebView view, String url)

Give the host application a chance to take over the control when a new url is about to be loaded in the current WebView. If WebViewClient is not provided, by default WebView will ask Activity Manager to choose the proper handler for the url. If WebViewClient is provided, return true means the host application handles the url, while return false means the current WebView handles the url. This method is not called for requests using the POST "method".

You can also consider the method shouldInterceptRequest().

bonnyz
  • 12,745
  • 5
  • 41
  • 64