Is it possible to launch a web browser from my application and return the clicked URL?

I wonder if it is possible to launch a web browser from my application and when the user clicks on a URL that follows a certain pattern, it returns that URL.

Example:

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

      

I will not do this because I am building an application with a built-in download system and I want the user to be able to easily select the file to download (just by clicking on the URL). The process looks like this:

  • You go to your personal page on the website.
  • You are viewing your personal files.
  • You choose the file you want to download.
  • The app will automatically download and save it for you when you click on the file url.

Step 4 is the step I am currently working on and cannot decide.
With an external browser, the process would look something like this:

  • You go to your personal page on the website.
  • You are viewing your personal files.
  • You choose the file you want to download.
  • You are copying the url.
  • Paste it into the app.

The process may not look more complicated, but I want to do it anyway.

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

+3


source to share


2 answers


You can intercept any type of URI WebView

with a custom one 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)

      

Let the host application take over when the new url is already loaded in the current WebView. If no WebViewClient is provided, by default the WebView will ask the Activity Manager to select the correct handler for the URL. If a WebViewClient is provided, return true means that the host application is processing the URL, and returning false means that the current WebView is processing the URL. This method is not named for POST requests.

You can also consider the shouldInterceptRequest () method .

+1


source


If you are manipulating a WebPage with links, you can achieve this with a custom Url Intent https://developer.chrome.com/multidevice/android/intents

This function is browser dependent, so Chrome will do, but maybe another browser won't. .. :(

Here you have some examples
Android will respond to url in intent
Run custom android app from android browser



Last time I tried with 50/60% browsers, so ...

+2


source







All Articles