Reputation: 3042
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:
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:
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?
Upvotes: 3
Views: 1202
Reputation: 5119
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......
Upvotes: 2
Reputation: 13548
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().
Upvotes: 1