如何解决err_unknown_url_scheme?

MainActivity.Java

package com.example.neermaicom;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import static android.content.Intent.*;

public class MainActivity extends AppCompatActivity {

private WebView webView;

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

    getSupportActionBar().hide();
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

    webView = findViewById(R.id.webview);
    webView.setWebViewClient(new WebViewClient()); // this will load site in our app
    webView.loadUrl("http://www.neermai.com");

    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
}

//This method require to use back button if want to go previous web page

@Override
public void onBackPressed() {

    if(webView.canGoBack()){
        webView.goBack();
    }else {
        super.onBackPressed();
    }
}

}

这是我的代码。工作正常。但仅当我通过单击社交媒体分享该帖子时才发出问题。网路上说:err_unknown_url_scheme。请帮我。谢谢

评论
  • 莫小言
    莫小言 回复

    HTML links that starts with mailto:, whatsapp: not starts with "http://" or "https://", so WebView cannot parse it to right place, we should use intent to redirect the url.

    So setWebViewClient to your WebView, like below and override shouldOverrideUrlLoading :

    webView.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;
            }
        }
        });
    

    而且你很好。

    希望这会帮助你。