从 Android 中的 HTML 页面拨打电话号码时获取 net::ERR_UNKNOWN_URL_SCHEME

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24697429/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 08:36:44  来源:igfitidea点击:

Getting net::ERR_UNKNOWN_URL_SCHEME while calling telephone number from HTML page in Android

androidhtmlerror-handlingandroid-manifest

提问by Karthik

I am getting "net::ERR_UNKNOWN_URL_SCHEME" while calling a telephone number option from an HTML page in Android. Do I need to add any permission(s) in the manifest to get this working? I haven't added anything in the manifest so far. Here's the HTML Code:

我在从 Android 的 HTML 页面调用电话号码选项时收到“net::ERR_UNKNOWN_URL_SCHEME”。我是否需要在清单中添加任何权限才能使其正常工作?到目前为止,我还没有在清单中添加任何内容。这是 HTML 代码:

<a href="tel:+1800229933">Call us free!</a>

<a href="tel:+1800229933">Call us free!</a>

回答by David M

The following should work and not require any permissions in the manifest (basically override shouldOverrideUrlLoading and handle links separately from tel, mailto, etc.):

以下应该可以工作并且不需要清单中的任何权限(基本上覆盖 shouldOverrideUrlLoading 并处理与 tel、mailto 等分开的链接):

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

    WebSettings webSettings = mWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

    mWebView.setWebViewClient(new WebViewClient(){
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if( url.startsWith("http:") || url.startsWith("https:") ) {
                return false;
            }

            // Otherwise allow the OS to handle things like tel, mailto, etc.
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity( intent );
            return true;
        }
    });
    mWebView.loadUrl(url);

Also, note that in the above snippet I am enabling JavaScript, which you will also most likely want, but if for some reason you don't, just remove those 2 lines.

另外,请注意,在上面的代码段中,我启用了 JavaScript,您也很可能需要它,但如果由于某种原因您不这样做,只需删除这 2 行。

回答by Sly_cardinal

I had this issue occurring with mailto:and tel:links inside an iframe (in Chrome, not a webview). Clicking the links would show the grey "page not found" page and inspecting the page showed it had a ERR_UNKNOWN_URL_SCHEME error.

我在iframe(在 Chrome 中,而不是 webview 中)中出现了这个问题mailto:tel:链接。单击链接将显示灰色的“找不到页面”页面,检查该页面会显示它有一个 ERR_UNKNOWN_URL_SCHEME 错误。

Adding target="_blank", as suggested by this discussion of the issuefixed the problem for me.

添加target="_blank",正如对问题的讨论所建议的那样为我解决了这个问题。

回答by Haresh Chhelana

Try this way,hope this will help you to solve your problem.

试试这个方法,希望这能帮助你解决你的问题。

main.xml

主文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:gravity="center">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

MyActivity.java

我的活动.java

public class MyActivity extends Activity {

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        webView = (WebView) findViewById(R.id.webView);
        webView.loadData("<a href=\"tel:+1800229933\">Call us free!</a>", "text/html", "utf-8");
    }

}

Please add this permission in AndroidManifest.xml

请在 AndroidManifest.xml 中添加此权限

<uses-permission android:name="android.permission.CALL_PHONE"/>