webview 应用程序 android 中的 Whatsapp 聊天无法正常工作

Whatsapp chat in webview app android not working

我有一个 webview android 应用程序,我在 webview 中使用的网站上有 whatsapp 选项,但是 whatsapp link 不起作用 我是 android 开发的新手我已经厌倦了 google 和 Whosebug 的许多其他方法,但问题仍然没有解决,有人可以帮忙吗?

import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.webkit.WebChromeClient;

public class MainActivity extends AppCompatActivity {
    private WebView mywebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mywebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings=mywebView.getSettings();
        mywebView.loadUrl("https://mywebsite.com");
        webSettings.setJavaScriptEnabled(true);
        mywebView.setWebViewClient(new WebViewClient());
    }
    public class myWebClient extends WebViewClient{
        @Override
        public void onPageStarted (WebView view, String url, Bitmap favicon){
            super.onPageStarted(view, url, favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url){
            view.loadUrl(url);
            return true;
        }
    }

    @Override
        public void onBackPressed() {
        if (mywebView.canGoBack()) {
            mywebView.goBack();
        }
            else {
                super.onBackPressed();
            }
    }
}

这里我自己修复的问题是我用来修复这个问题的代码。

解决方法如下:

public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url == null) {
                return false;
            }
            if (url.startsWith("market://")) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
            if (url.startsWith("https://api")) {
                view.getContext().startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                return true;
            }
            if (url.startsWith("mailto:")) {

                try {
                    List<String> to = new ArrayList<String>();
                    List<String> cc = new ArrayList<String>();
                    List<String> bcc = new ArrayList<String>();
                    String subject = null;
                    String body = null;

                    url = url.replaceFirst("mailto:", "");

                    String[] urlSections = url.split("&");
                    if (urlSections.length >= 2) {

                        to.addAll(Arrays.asList(urlSections[0].split(",")));

                        for (int i = 1; i < urlSections.length; i++) {
                            String urlSection = urlSections[i];
                            String[] keyValue = urlSection.split("=");

                            if (keyValue.length == 2) {
                                String key = keyValue[0];
                                String value = keyValue[1];

                                value = URLDecoder.decode(value, "UTF-8");

                                if (key.equals("cc")) {
                                    cc.addAll(Arrays.asList(url.split(",")));
                                }
                                else if (key.equals("bcc")) {
                                    bcc.addAll(Arrays.asList(url.split(",")));
                                }
                                else if (key.equals("subject")) {
                                    subject = value;
                                }
                                else if (key.equals("body")) {
                                    body = value;
                                }
                            }
                        }
                    }
                    else {
                        to.addAll(Arrays.asList(url.split(",")));
                    }

                    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
                    emailIntent.setType("message/rfc822");

                    String[] dummyStringArray = new String[0]; // For list to array conversion
                    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, to.toArray(dummyStringArray));
                    if (cc.size() > 0) {
                        emailIntent.putExtra(android.content.Intent.EXTRA_CC, cc.toArray(dummyStringArray));
                    }
                    if (bcc.size() > 0) {
                        emailIntent.putExtra(android.content.Intent.EXTRA_BCC, bcc.toArray(dummyStringArray));
                    }
                    if (subject != null) {
                        emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
                    }
                    if (body != null) {
                        emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);
                    }
                    view.getContext().startActivity(emailIntent);

                    return true;
                }
                catch (UnsupportedEncodingException e) {
                    /* Won't happen*/
                }

            }
            else {
                view.loadUrl(url);
                return true;
            }
            return false;
        }