如何在我的webView外部打开外部链接?

时间:2019-02-07 00:27:06

标签: android android-webview

我用php创建了一个网站,并使用webView作为android应用程序打开,我使用网站上的外部链接打开whatsapp链接。但是,仅当浏览器访问指向我的whatsapp的链接时才起作用,但在我的webView中的链接不起作用。 出现网:: ERR_UNKNOWN_URL_SCHEME,如何解决?

   MainActivity
package com.example.programmingknowledge.sitehc;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.programmingknowledge.easyonlineconverter.R;


public class MainActivity extends Activity {
    private WebView myWebView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView)findViewById(R.id.myWebView);
        WebSettings webSettings = myWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        myWebView.loadUrl("http://mysite/index.php#"); // 
        //myWebView.getSettings().setBuiltInZoomControls(true); //acrescentei zoom
       // myWebView.getSettings().setUseWideViewPort(true);
//AQUI BOTEI

        //AQUI TIREI
        myWebView.setWebViewClient(new WebViewClient(){
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl){
                myWebView.loadUrl("file:///android_asset/error.html");  //file:///android_asset/error.html

            }
        });
    }

    @Override
    public void onBackPressed() {
        if(myWebView.canGoBack()) {
            myWebView.goBack();
        } else {
            myWebView.goBack();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:0)

您必须使用条件为url的自定义webview客户端来检查当前url是否包含匹配的文本

class MyWebView extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url.contains("whatsapp")) {
                Intent launchIntent = getPackageManager().getLaunchIntentForPackage("com.whatsapp");
                startActivity(launchIntent);
            } else {
                view.loadUrl(url);
            }
            return true;
        }
    }

为此,您必须在URL中添加“ whatsapp” 字样才能打开应用程序。