Android WebView err_unknown_url_scheme

盛年不重来,一日难再晨,及时当勉励,岁月不待人。这篇文章主要讲述Android WebView err_unknown_url_scheme相关的知识,希望能为你提供帮助。
使用下面简单的代码我可以正确加载我的url,但是,当我尝试点击以mailto:whatsapp:和tg:(Telegram)开头的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"); } }

答案【Android WebView err_unknown_url_scheme】您必须在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; }

另一答案您需要覆盖WebViewClient的方法shouldOverrideUrlLoading,您可以在其中自行控制链接传输。
因为html links that starts with mailto: whatsapp: and tg: (Telegram).不是常见的url以“http://”或“https://”开头,所以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"); }}

另一答案实际上WebView没有如何使用url方案,如mailto,tg,sms,phone。您应该覆盖shouldOverrideUrlloading()方法,并在找到这些类型的方案时执行webview需要执行的操作。
@Override public boolean shouldOverrideUrlLoading(WebView view, Stringurl) {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; }

在API级别24中不推荐使用shouldOverrideUrlLoading(WebView视图,String url)。
所以重写public boolean 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]
另一答案mailto链接不会被加载到你的webview.you在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

    推荐阅读