Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Launch custom Android application from WebView

I have an HTML file which is launching an app if I open it in the Android native browser, but when I try to open the same in a WebView, it is not able to launch that application, and "Webpage not available" is shown. I think my WebView is not able to handle the scheme "my.special.scheme://" defined for the application.

I read Launching an Android Application from the Browser, but it does not cover information about launching an app from a WebView.

like image 378
Sunny Kumar Aditya Avatar asked Jun 28 '12 10:06

Sunny Kumar Aditya


People also ask

How do I send a post request in WebView?

How do I send a post request in WebView? setWebViewClient(new WebViewClient(){ public void onPageStarted(WebView view, String url, Bitmap favicon) { super. onPageStarted(view, url, favicon); } public boolean shouldOverrideUrlLoading(WebView view, String url) { webView. postUrl(Base_Url, postData.


2 Answers

It's true, links with a custom URI scheme don't load automatically launch apps from a WebView.

What you need to do is add a custom WebViewClient to your WebView:

webView.setWebViewClient(new CustomWebViewClient());

and then in the shouldOverrideUrlLoading(), have the following code:

public boolean shouldOverrideUrlLoading(final WebView webView, final String url) {

    if (url.startsWith("my.special.scheme://")) {

        final Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));

        // The following flags launch the app outside the current app
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);

        activity.startActivity(intent);

        return true;
    }  

    return false;
}
like image 94
jklp Avatar answered Sep 23 '22 06:09

jklp


I'm not sure, but I believe that WebView simply doesn't handle custom URI schemes.

The workaround is to override WebViewClient.shouldOverrideUrlLoading() and manually test if the URL uses your URI scheme, launching your app and returning true if it matches, otherwise returning false.

like image 40
Darshan Rivka Whittle Avatar answered Sep 24 '22 06:09

Darshan Rivka Whittle