Android WebView err_unknown_url_scheme

时间:2017-01-17 09:22:19

标签: android html webview

使用下面的简单代码,我可以正确加载我的网址,但是,当我尝试点击以 mailto: whatsapp开头的html链接时,我会收到“ERR_UNKNOWN_URL_SCHEME”: tg:(电报)。

任何人都可以帮我解决这个问题吗?不幸的是我根本不懂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");
    }
}

6 个答案:

答案 0 :(得分:10)

您必须在Webview中设置客户端并将其传递给意图

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

答案 1 :(得分:2)

您需要重写WebViewClient的方法shouldOverrideUrlLoading,在其中您可以自己控制链接的传输。

由于html links that starts with mailto: whatsapp: and tg: (Telegram).不是以“ http://”或“ https://”开头的通用网址,因此WebView无法将其解析到正确的位置,因此我们应使用意图重定向网址。

例如:

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

然后将setWebViewClient设置为您的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");
}}

答案 2 :(得分:2)

实际上,WebView不会使用mailto,tg,sms,phone之类的URL方案。您应该重写shouldOverrideUrlloading()方法,并在找到这些类型的方案时执行您的Web视图所需的操作。

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

            if( URLUtil.isNetworkUrl(url) )
            {
                return false;
            }
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }catch(ActivityNotFoundException e)
            {
                Log.e("AndroiRide",e.toString());
                Toast.makeText(MainActivity.this,"No activity found",Toast.LENGTH_LONG).show();
            }

            return true;
        }

shouldOverrideUrlLoading(WebView视图,字符串url)在API级别24中已弃用。

因此请覆盖布尔布尔值布尔值ShouldOverrideUrlLoading(WebView视图,WebResourceRequest请求)

@RequiresApi(Build.VERSION_CODES.N)
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request)
        {
            String url=request.getUrl().toString();
            if( URLUtil.isNetworkUrl(url) )
            {
                return false;
            }
            try {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setData(Uri.parse(url));
                startActivity(intent);

            }catch(ActivityNotFoundException e)
            {
                Log.e("AndroiRide",e.toString());
                Toast.makeText(MainActivity.this,"No activity found",Toast.LENGTH_LONG).show();
            }

            return true;
        }

如果您创建自己的方案,请自定义代码。 [ERR unknown URL Scheme in Android WebView - Kotlin & Java code]

答案 3 :(得分:1)

@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:邮件收件人:

答案 4 :(得分:0)

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

答案 5 :(得分:0)

为我工作

  

“ webview.setWebViewClient(new WebViewClient(){              字符串currentUrl;

       @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