I'm trying to implement the following behavior into my Android App.
The app is a native app with some parts using WebView, in this WebView there is the possibility of using different payment methods.
If the user has the app of that bank installed I want to be able to open the corresponding app. In some cases, the website loaded into the WebView launch a specific intent:// that I can easily intercept and try to redirect the user to the app, but for the cases where the website use the new Android App link I'm not able to intercept them because they are normal https:// calls.
I tried to load the Android App link into Chrome and this way the app is opened.
My question at this point is how I can replicate the behavior of Chrome into my WebView??
I didn't find that much informations on this specific case besides the Android Documentation
Thanks for your help :)
You can intercept the url click by using custom WebViewClient for the webview.
1)Set the custom webview client for the webview
2)Override the below method and return true (you can do this based on particular url also. )
shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
3)Override below method and handle the click and start the activity using either package name or playstore url with intent.
public void onLoadResource (WebView view,
String url)
WebView mWebView;
WebViewClient customWebClient = new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request){
//This can be modified based on the url . URL can be retrieved using request.getUrl()
return true;
}
#Override
public void onLoadResource(WebView view, String url){
if( url.equals("") ){
// launch playstore activity
final String appPackageName = "package name of the application"
try {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=" + appPackageName)));
} catch (android.content.ActivityNotFoundException anfe) {
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + appPackageName)));
}
}
}
}
mWebView.setWebViewClient(customWebClient);
Related
I created a WebView App and it's working fine but there is a link which I want it to open in the default browser instead of that app what can I do.
Add a WebViewClient to your webView (if not already added) and then override shouldOverrideUrlLoading () method:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView webview, String url) {
if (url.contains(myUrl)) {
Intent intent= new Intent(Intent.ACTION_VIEW, myUrl);
context.startActivity(intent);
return true;
} else {
return super.shouldOverrideUrlLoading(webview, url);
}
}
}
This way your are telling your webview to not continue loading a specific url. Instead, launch the proper application (mostly a browser) to handle the url.
FB share button could successfully oepn the FB messenger app in Chrome browser.
However, the same source code, it fail to open the FB messenger app if display in Android APP application, and this application display web page using Chrome browser inside.
How to fix it?
You could provide a custom WebViewClient implementation to your WebView that checks for facebook.com links (or whatever links you like) and then explicitly fire an Intent so others can pickup the Action, instead of allowing the WebView to handle it as it sees fit.
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
Uri uri = Uri.parse(url);
if (uri.getHost().contains("facebook.com")) {
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
return false;
}
} catch(Exception e){
e.printStackTrace();
}
return super.shouldOverrideUrlLoading(view, url);
}
});
so I just started with Android programming and I am trying to make a little app using WebView. There is a url that redirects you to a pdf, I know WebView does not render pdf. So I want to use intent and display the pdf in Google Docs. However, the pdf address is randomly generated so I cant link it with
WebView.loadUrl("http://docs.google.com/gview?embedded=true&url=" + pdfURL);
How can I send an intent to Google Docs without using the exact pdf address?
I don't know what "randomly generated" means.
But the first thing that comes to my mind is to set a WebViewClient and override shouldOverrideUrlLoading:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.endsWith(".pdf") == true) {
view.loadUrl("http://docs.google.com/gview?embedded=true&url=" + url);
return true;
}
return false;
}
});
Some more info in this thread.
I have a webview with custom webview client. I'm intercepting requests with:
#Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
// ...
}
sometimes I'm getting informations about custom events from webview in a form of a link:
xyz://do-something
When it happens I want webview to ignore it. But 'shouldInterceptRequest' has to return something and when I'm returning 'null' instead of my page it shows: 'unknown url xyz://do-something'. How can I deal with that? How can I intercept the link but disable action on webview side?
You just need to do a string comparison from the site that you are in and the site that you want to go to.
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("www.example.com")) {
// This is my web site, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
I'm currently developing an application with android WebView.
However, the website has a link that will invoke Skype application.
The link work fine when try with Chrome browser on android, but it return an error when try to open in android WebView. Actually, it gave me an Webpage not Available error.
Any advice?
Regards.
You should check the url that is click/opened in the webview and check if there is an application that handles this kind of urls:
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url == skype url) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
} else {
view.loadUrl(url);
}
return true;
}
});