i am trying to pass a url which start with www.example.com/xxxxxxx in my webview it can be anything in the place of xxxxxxx. see the code you will understand
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://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 want to pass any url from my website only else open it in default browser.
Uri.getHost() return the host from the authority, you should try to use if (Uri.parse(url).getHost().equals("www.example.com")) or if (Uri.parse(url).getHost().equals("example.com")).
Also, as a good practice, you should reverse your equals() statement like this: "example.com".equals(Uri.parse(url).getHost()) as you know that "example.com" will never be null and, as such, never throw a NullPointerException.
Related
When external links are clicked on my website, I want them to open in the application it is connected to. But I am getting page not found error. For example, when instagram.com is clicked, it will open on Instagram.
You need to define a webView CLient like this:
private class MyWebViewClient extends WebViewClient {
//link opener
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if ("yourHost.com".equals(Uri.parse(url).getHost()))
{
// This is my website, so do not override; let my WebView load the page
//loading.show();
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;
}
}
on set this webViewClient in your webView like: mywebView.setWebViewClient(new MyWebViewClient())
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.
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;
}
Following as reference this post
webview, download file on finish prepared document
I think of a possible solution, but as it is not the same doubt.
Them my possible solution as "intercept" POST header in concrete and redirect to intent.ACTION_VIEW
But can't find some example.
#Override
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 have WebView in which I want to open links belong to domain www.example.org in webview while all other links (if clicked) open by the default browser outside of my application.
I tried to use public boolean shouldOverrideUrlLoading(WebView view, String url) but it does not work properly.
Here is the code that does not work:
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if (urlObj.getHost().equals("192.168.1.34")) {
view.loadUrl(url);
return true;
} else {
view.loadUrl(url);
return false;
}
} catch (Exception e) {
}
}
}
In both cases ( return true and return false) the URL is handled by my application.
Once you create and attach a WebViewClient to your WebView, you have overridden the default behavior where Android will allow the ActivityManager to pass the URL to the browser (this only occurs when no client is set on the view), see the docs on the method for more.
Once you have attached a WebViewClient, returning false form shouldOverrideUrlLoading() passes the url to the WebView, while returning true tells the WebView to do nothing...because your application will take care of it. Unfortunately, neither of those paths leads to letting Android pass the URL to the browser. Something like this should solve your issue:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
try {
URL urlObj = new URL(url);
if( TextUtils.equals(urlObj.getHost(),"192.168.1.34") ) {
//Allow the WebView in your application to do its thing
return false;
} else {
//Pass it to the system, doesn't match your domain
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
//Tell the WebView you took care of it.
return true;
}
}
catch (Exception e) {
e.printStackTrace();
}
}
I know that seems a little counterintuitive as you would expect return false; to completely circumvent the WebView, but this is not the case once you are using a custom WebViewClient.
Hope that helps!
If you can't be bothered to explain what "does not work properly" means, we can't be bothered to give you much specific help.
Use shouldOverrideUrlLoading(). Examine the supplied URL. If it is one you want to keep in the WebView, call loadUrl() on the WebView with the URL and return true. Otherwise, return false and let Android handle it normally.
Add the following to your activity
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith("192.168.1.34")) {
view.loadUrl(url);
Log.d("URL => ", url); // load URL in webview
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent); // Pass it to the system, doesn't match your domain
return true;
}