Android WebView错误未知URL方案

h6my8fg2  于 2023-01-19  发布在  Android
关注(0)|答案(6)|浏览(195)

使用下面的简单代码,我可以正确加载我的URL,但是,当尝试点击以 * mailto:whatsapp: 和 * tg:(电报)开头的html链接时,我会得到"ERR_UNKNOWN_URL_SCHEME"。
任何人都可以帮助我解决这个问题吗?不幸的是,我根本不知道Java:(
谢谢。

import android.app.Activity;
    import android.os.Bundle;
    import android.webkit.WebSettings;
    import android.webkit.WebView;
    import android.webkit.WebViewClient;
        
    public class MainActivity extends Activity {
    
        private WebView mWebView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mWebView = (WebView) findViewById(R.id.activity_main_webview);
    
            // Force links and redirects to open in the WebView instead of in a browser
            mWebView.setWebViewClient(new WebViewClient());
    
            // Enable Javascript
            WebSettings webSettings = mWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);
    
            // Use remote resource
            mWebView.loadUrl("http://myexample.com");
        }
    }
jaxagkaj

jaxagkaj1#

@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;
        }

把这个代码放在你的mWebView中。setWebViewClient(new WebViewClient().它将完美地工作于所有的链接,如tel:whatsapp:,**mailto:**等。

pgvzfuti

pgvzfuti2#

这对我有用

“webview.setWebViewClient(新建WebViewClient(){字符串当前URL;

@Override
       public boolean shouldOverrideUrlLoading(WebView view, String url) {
           currentUrl = url;

           if (url.startsWith("http") || url.startsWith("https")) {
               return false;
           }
           if (url.startsWith("intent")) {



             try {
                 Intent intent = Intent.parseUri(url, Intent.URI_INTENT_SCHEME);

                 String fallbackUrl = intent.getStringExtra("browser_fallback_url");
               if (fallbackUrl != null) {
                   webview.loadUrl(fallbackUrl);
                   return true;
               }}

           catch (URISyntaxException e) {
               //not an intent uri
           }
   return true;//do nothing in other cases
ut6juiuv

ut6juiuv3#

mailto链接将不会加载到您的webview中。您必须在shouldOverrideUrlLoading中进行检查,并使用intent进行处理。

public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (url.startsWith("mailto:")) {

        Intent share = new Intent(Intent.ACTION_SEND);
        share.setType("text/plain");
        share.putExtra(Intent.EXTRA_TEXT, message);
        startActivity(Intent.createChooser(share, "Title of the dialog the system will open"));
        view.reload();
        return true;
    }
  }

类似问题Android Webview ERR_UNKNOWN_URL_SCHEME Error

rqcrx0a6

rqcrx0a64#

您可以打开外部应用程序(Chrome)的URL来解决这个问题。

2wnc66cl

2wnc66cl5#

您必须在webview中设置客户端并将其传递给Intent

webView.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( URLUtil.isNetworkUrl(url) ) {
                return false;
            }
            if (appInstalledOrNot(url)) {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity( intent );
            } else {
                // do something if app is not installed
            }
            return true;
        }

    });
}

你可以有一个方法来检查是否安装了应用程序

private boolean appInstalledOrNot(String uri) {
        PackageManager pm = getPackageManager();
        try {
            pm.getPackageInfo(uri, PackageManager.GET_ACTIVITIES);
            return true;
        } catch (PackageManager.NameNotFoundException e) {
        }

        return false;
    }
13z8s7eq

13z8s7eq6#

您需要覆盖WebViewClient的shouldOverrideUrlLoading方法,在该方法中您可以自己控制链接传输。
由于html links that starts with mailto: whatsapp: and tg: (Telegram).不是以"http://"或" https://"开头的常见URL,因此WebView无法将其解析到正确的位置,我们应使用Intent重定向URL。
例如:

@Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

                try {
                    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                    view.getContext().startActivity(intent);
                    return true;
                } catch (Exception e) {
                    Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
                    return true;
                }
            }

然后将WebViewClient设置为您的WebView,如下所示:

public class MainActivity extends Activity {

    private WebView mWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mWebView = (WebView) findViewById(R.id.activity_main_webview);

        // Force links and redirects to open in the WebView instead of in a browser
        mWebView.setWebViewClient(new WebViewClient() {

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url == null || url.startsWith("http://") || url.startsWith("https://")) return false;

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                view.getContext().startActivity(intent);
                return true;
            } catch (Exception e) {
                Log.i(TAG, "shouldOverrideUrlLoading Exception:" + e);
                return true;
            }
        }
        });

        // Enable Javascript
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        // Use remote resource
        mWebView.loadUrl("http://myexample.com");
    }}

相关问题