I've created a web view app, the page that is displayed features market:// links but upon clicking them I get the 404 screen along with the error that the protocol is not supported. I've tried looking through documentation but was unable to find anything relating to this. Any help is much appreciated.
For me the JavaScript thing wasn't a solution as the HTML is not under my control. So if you need to control this from the application side, then there is a relative simple solution: Derive from WebViewClientand inject the implementation using WebView.setWebViewClient(). All you need to override in your WebViewClientimplementation is the shouldOverrideUrlLoading method as shown here:
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("market://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
For me this works fine.
HOPE THIS HELPS YOU
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
if (url.startsWith("market://")||url.startsWith("vnd:youtube")||url.startsWith("tel:")||url.startsWith("mailto:"))
{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(url));
startActivity(intent);
return true;
}
else{
view.loadUrl(url);
return true;
}
}
For the links to work you have to have the market app installed on your device/emulator.
Also your app need to request a permission to access network.
UPD:
as a workaround you can call java code from within the webview, for example if you generate links like this:
..
Define a javascript function named go():
<script type="text/javascript">
function go(link) {
if (handler) {
handler.go(link);
} else {
document.location = link;
}
}
</script>
You then can pass in a handler object into the WebView:
webview.addJavascriptInterface(new Handler() {
#Override
public void go(String marketUrl) {
//start market intent here
}
}, "handler");
Handler interface can be defined as follows:
public interface Handler{
public void go(String url);
}
Work for me:
webView = (WebView) findViewById(R.id.webView);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebViewClient(new MyWebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("http://myweb.com");
private class MyWebViewClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url != null && url.startsWith("whatsapp://")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
} else {
return false;
}
}
}
It is important to understand how the webview and its clients (webviewclient and webchromeclient) works. Please go through the http://therockncoder.blogspot.in/2014/04/understanding-androids-webchromeclient.html
In the webviewclient's shouldOverrideUrlLoading() method, you can decide if you want to open the link in new browser or within the webview. If you don't override this method, it will by default open the link in a new browser outside of the your android application.
If you want to open within webview, override the method as below
public boolean shouldOverrideUrlLoading(WebView view, String url) { <br>
Log.v("activity", "INSIDE WEBVIEW CLIENT ON shouldOverrideUrlLoading");
view.loadUrl(url);
return false; //need to understand return value based on usage
}
Schemes like whatsapp://send?text=Hello%20World! or market://details?id=xx.xx.xx will open the corresponding apps automatically if they are opened outside the webview and if that app is installed on the handset.
If you want to open certain links within webview and specific schemes outside webview, you need to override WebChromeClients onCreateWindow() method as explained in the link provided above. It should solve the purpose.
Instead of adding check for particular scheme, Modifying #sven solution, this will work for all schemes
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host= Uri.parse(url).getHost();
if (host == null) {
view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
return true;
}
view.loadUrl(url);
return false;
}
Simplest solution
Intent newApp = new Intent(Intent.ACTION_VIEW, Uri.parse(URL));
startActivity(newApp);
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.
I've found an app's source code and it opens web pages using external browsers, like Chrome or others. I want to make it access those pages from inside the app, without using an external browser. I think that I should change the code below somehow, but I don't know how.
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (Uri.parse(url).getHost().equals("http://dmc.teiion.gr")) {
//open url contents in webview
return false;
} else {
//here open external links in external browser or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
yourWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
view.loadUrl(url);
return true;
}
});
Or change
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
to
view.loadUrl(url);
I have a WebView in which I load a page with a custom link (like app://action). I registered the url schemes in the manifest file and when I click on the link, the onResume() method of my Activity is called with the correct data and it works OK.
My problem is that the WebView still try to load the link and my WebView ends up to show a "Web page unavailable" message. I don't want that.
How can I prevent the WebView to load the url?
Here's my code :
WebView banner = ...
banner.setWebViewClient(new WebViewClient() {
#Override
public void onLoadResource(WebView view, String url) {
if (url.startsWith("app://")) {
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(url), getContext(), Main.class);
//startActivity(i);
}
}
}
banner.loadUrl("url_to_the_banner");
Use WebViewClient.shouldOverrideUrlLoading instead.
public boolean shouldOverrideUrlLoading(WebView view, String url){
// handle by yourself
return true;
}
WebViewClient Reference
Updates: Method shouldOverrideUrlLoading(WebView, String) is deprecated in API level 24. Use shouldOverrideUrlLoading(WebView, WebResourceRequest) instead.
But it must return false otherwise, so:
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url){
if(url.startsWith(myString){
// handle by yourself
return true;
}
// ...
return false;
}
I am using webviewclient to open the html page. The html page is having a anchor tag.
when i click on the anchor tag my phone dialer activity should be launched.
when i click on this anchor tag in external browser (android default browser ), it is launching the phone dialer, but as i am using the webviewclient (browser with in my application). i am unable to launch the phone dialer.
is there any way to achieve this using webviewclient ?
You should override this method
public boolean shouldOverrideUrlLoading(WebView wv, String url)
{
if(isNumber)
{
//Get the number from the URL
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:12345"));
startActivity(intent);
return true;
}
return false;
}
in the WebViewClient, and return ture that means you want to handle this by yourself instead of the webView.
The document is here.
I think is the best way to do this. So Please try this, I know i am too late to reply this:
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if( URLUtil.isNetworkUrl(url) ) {
view.loadUrl(url);
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity( intent );
return true; /*
return false;*/
}
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;
}