Android WebView err_unknown_url_scheme

With the simple code below, I can correctly load my url, but I get "ERR_UNKNOWN_URL_SCHEME" when trying to use html links that start with mailto: whatsapp: and tg: (Telegram).

Can anyone help me fix this please? Unfortunately, I don’t know Java at all :(

Thanks.

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"); } } 
+14
source share
5 answers

You must install the client in a web browser and pass it on to the 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; } }); } 

You may have a way to check if the application is installed

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

You need to override the shouldOverrideUrlLoading method of the shouldOverrideUrlLoading object, in which you can independently control the transfer of links.

Because html links that starts with mailto: whatsapp: and tg: (Telegram). is not a common URL starting with "http: //" or "https: //", so WebView cannot parse it in the right place, so we must use the intent to redirect the URL.

For instance:

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

then set the WebViewClient for your WebView, like so:

 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"); }} 
+4
source

In fact, WebView is not able to work with URL schemes, such as mailto, tg, sms, phone. You must override the shouldOverrideUrlloading () method and do what your web view needs when this type of circuit is found.

  @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, String URL) is deprecated at API level 24.

Therefore, override the public logical shouldOverrideUrlLoading (WebView, WebResourceRequest request)

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

Customize the code if you are creating your own schemas. [ ERR unknown URL scheme in Android WebView - Kotlin & Java code]

+2
source

mailto links will not be uploaded to your webview . You check it like this in shouldOverrideUrlLoading and process it with 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; } } 

Similar question Android Webview ERR_UNKNOWN_URL_SCHEME Error

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

Put this code in your mWebView.setWebViewClient (new WebViewClient () . It will work fine for all links like tel:, whatsapp:, mailto: etc.

0
source

Source: https://habr.com/ru/post/1262917/


All Articles