Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Support for other protocols in Android webview

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.

like image 728
user319940 Avatar asked Aug 27 '10 10:08

user319940


People also ask

What is alternative of WebView in Android?

Alternatives to WebView If you want to send users to a mobile site, build a progressive web app (PWA). If you want to display third-party web content, send an intent to installed web browsers. If you want to avoid leaving your app to open the browser, or if you want to customize the browser's UI, use Custom Tabs.

What is WebView support?

Android WebView is a system component for the Android operating system (OS) that allows Android apps to display content from the web directly inside an application.

Which of the following is enabled by default in Android WebView?

The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.

Is WebView deprecated in Android?

This interface was deprecated in API level 12. This interface is now obsolete.


3 Answers

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.

like image 150
sven Avatar answered Oct 16 '22 20:10

sven


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;
        }
}
like image 43
Sathish Kumar Avatar answered Oct 16 '22 19:10

Sathish Kumar


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:

<a href="javascript:go('market://your.path.to.market.app')">..</a>

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);

}
like image 1
Konstantin Burov Avatar answered Oct 16 '22 20:10

Konstantin Burov