Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I open whatsapp app from android webview

I have made a web-view of a website. Now I want to share some data from my web-view to whatsapp app. I was able to open whatsapp web, but my client wants me to open whatsapp application instead of whatsapp web. How can I do that?

This is what I have done to open whatsapp web in my site:

<a class="social-icon whatsapp" 
  href="javascript:popWin('https://api.whatsapp.com/send?text=Product%20Name:-<?php echo $productName; ?>.%20Product%20link:-<?php echo $productUrl; ?>', 'whatsapp', 'width=640, height=480, left=0, top=0, location=no, status=yes, scrollbars=yes, resizable=yes');"
  title="<?php echo $this->__('Share on Whatsapp') ?>" 
  data-action="share/whatsapp/share">
  <span><i class="fa fa-whatsapp"></i></span>
</a>
like image 726
Abhishek Kalotra Avatar asked Jun 06 '19 04:06

Abhishek Kalotra


4 Answers

@Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                return true;
            }
            return false;
        }

Put this code in your mWebView.setWebViewClient(new WebViewClient(). it will be working perfectly for all link like tel:, whatsapp: etc.

like image 144
NSMedia Solution Avatar answered Oct 13 '22 11:10

NSMedia Solution


Use this and works ok. (@NSMedia-Solution) Just put

'myWebView.goBack();'

before return (For not navigate in whatsApp web)

myWebView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView wv, String url) {
                if(url.startsWith("tel:") || url.startsWith("whatsapp:")) {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    intent.setData(Uri.parse(url));
                    startActivity(intent);
                    myWebView.goBack();
                    return true;
                }
                return false;
            }
        });
like image 45
perezmirabile Avatar answered Oct 13 '22 13:10

perezmirabile


   webView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView wv, String url) {
            if(url.startsWith("tel:") || url.startsWith("whatsapp:") || url.startsWith("intent://") || url.startsWith("http://") ) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);
                webView.goBack();
                return true;
            }
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);

            invalidateOptionsMenu();
        }

        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            try {
                invalidateOptionsMenu();
            } catch (Exception e) {

            }

            if (webView.canGoBack()) {
                webView.goBack();
            }

        }


        public void onPageFinished(WebView view, String url) {
            //     pullToRefresh.setRefreshing(false);

            invalidateOptionsMenu();
        }



    });

my problem solves using this code.

like image 4
mahedi hassan Avatar answered Oct 13 '22 11:10

mahedi hassan


You can use, its working for me:

Option First-

<a href="whatsapp://send?text=Hello friend!" data-action="share/whatsapp/share">Share</a>

Option Second-

<a href="https://api.whatsapp.com/send?text=Hello friend!" data-action="share/whatsapp/share">Share</a> 

Both options are working for me..

If not working to your side then please update android version of your mobile phone & also update whatsapp version

like image 3
Rakesh P Avatar answered Oct 13 '22 11:10

Rakesh P