Saki
Saki

Reputation: 1777

How to open external app from webview in android app after a button is clicked in the webview?

I HAVE ALREADY TRIED How to handle intent:// on a webView URL? This solution just opens up a Google Play Store Web Page in my WebView, what I want is on the click, another app should open whose intent is provided as happens in Chrome.

My app has a WebView that works fine. At one stage, the WebView requests access to the following(an external payment app):

intent://pay/?pa=digitalshowroom1.payu@indus&pn=DOTPE%20PRIVATE%20LIMITED&tr=13261664955&tid=CX0FOrvSrHzDh7gP&am=1.00&cu=INR&tn=OrderId-CX0FOrvSrHzDh7gP

When I use the same website that my WebView is using in Chrome, it opens the external payment app successfully, i.e. Chrome is able to handle that intent, how can my app handle the same intent.

I seem to know I have to use

public boolean shouldOverrideUrlLoading(WebView view, String url)

for this, and I'm using it as follows:

public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null && (url.startsWith("whatsapp://") || url.startsWith("tel") || url.startsWith("market"))) {
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                }

else {
return false;

                }

Now, this piece of code is handling the intents like opening WhatsApp, Calling app, etc. fine as they start with whatsapp or tel, so I'm able to leverage the url.startsWith() method.

How can I handle the links that request access to external apps and start with intent://?

Thanks in advance!

Upvotes: 2

Views: 1958

Answers (2)

user15312910
user15312910

Reputation:

I tried something similar and found you have to build the URI like this and handle all URL parameters and call the build method. I did it for a UPI payments app. Call this method in shouldOverrideUrlLoading().

public void openPaymentApp(final String url)
    {

        try {

            String pa= url.substring(url.indexOf("pa=")+3,url.indexOf("&pn"));
            String pn=url.substring(url.indexOf("pn=")+3,url.indexOf("&tr"));
            String tr=url.substring(url.indexOf("tr=")+3,url.indexOf("&tid"));
            String tid=url.substring(url.indexOf("tid=")+4,url.indexOf("&am"));
            String am=url.substring(url.indexOf("am=")+3,url.indexOf("&cu"));
            String cu=url.substring(url.indexOf("cu=")+3,url.indexOf("&tn"));
            String tn=url.substring(url.indexOf("tn=")+3,url.indexOf("#Intent"));


            Uri uri =
                    new Uri.Builder()
                            .scheme("upi")
                            .authority("pay")
                            .appendQueryParameter("pa",pa)
                            .appendQueryParameter("pn",pn)
                            .appendQueryParameter("tr",tr)
                            .appendQueryParameter("tid",tid)
                            .appendQueryParameter("am",am)
                            .appendQueryParameter("cu",cu)
                            .appendQueryParameter("tn",tn)
                            .build();
            Intent launchIntent = getPackageManager()
                    .getLaunchIntentForPackage( "com.package.name");
            launchIntent.setData(uri);
            startActivity( launchIntent );



        } catch (Exception e) {
            e.printStackTrace();
            Toast.makeText(getApplicationContext(),"Coudln't open", Toast.LENGTH_SHORT).show();
        }


    }

Upvotes: 1

Praful Patel
Praful Patel

Reputation: 219

Use below code:

Uri uri = Uri.parse(url);
        if (uri.getScheme().equals("intent")) {
            String appPackage = getAppPackageFromUri(uri);

            if (appPackage != null) {
                PackageManager manager = getContext().getPackageManager();
                Intent appIntent = manager.getLaunchIntentForPackage(appPackage);

                if (appIntent != null) {
                    getActivity().startActivity(appIntent);
                } else {
                    // open external website
                }
            }
        }

Please use code if any issue let me know

Happy coding!

Upvotes: 0

Related Questions