如何使我的webview停止在浏览器上打开某些链接

时间:2016-07-04 07:33:16

标签: android webview android-webview

package net.developerbase.android.app;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.webkit.WebView;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String uas = "AppleWebKit/537.36" + "(KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36";
        final WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setBlockNetworkLoads(false);
        view.getSettings().setAppCacheEnabled(true);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setAllowUniversalAccessFromFileURLs(true);
        view.getSettings().setGeolocationEnabled(true);
        view.getSettings().setUserAgentString(uas);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setDatabaseEnabled(true);
        view.getSettings().setLoadsImagesAutomatically(true);
        view.getSettings().setLoadWithOverviewMode(true);
        view.getSettings().setUseWideViewPort(true);
        view.getSettings().setDatabaseEnabled(true);
        view.getSettings().setDomStorageEnabled(true);
        view.loadUrl("http://58.182.126.214:4567");
            }

}

如何制作,以便我的应用在应用和浏览器上的其他链接上保留一定的链接。

请帮助我将您的代码添加到我的代码中,因为我仍然是Android编程的菜鸟。谢谢:D

1 个答案:

答案 0 :(得分:0)

我认为你需要WebViewClient的功能WebViewClient shouldOverrideUrlLoading()

private class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    if (Uri.parse(url).getHost().equals("www.example.com")) {
        // This is my web site, so do not override; let my WebView load the page
        return false;
    }
    // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
    Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
    startActivity(intent);
    return true;
}
}

要将WebViewClient与WebView连接,只需设置它:

WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());

此链接应该有用https://developer.android.com/guide/webapps/webview.html#HandlingNavigation

*编辑*

 public class MainActivity extends AppCompatActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String uas = "AppleWebKit/537.36" + "(KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36";
        final WebView view = (WebView) this.findViewById(R.id.webView);
        view.getSettings().setBlockNetworkLoads(false);
        view.getSettings().setAppCacheEnabled(true);
        view.getSettings().setJavaScriptEnabled(true);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setAllowFileAccess(true);
        view.getSettings().setAllowUniversalAccessFromFileURLs(true);
        view.getSettings().setGeolocationEnabled(true);
        view.getSettings().setUserAgentString(uas);
        view.getSettings().setAllowContentAccess(true);
        view.getSettings().setDatabaseEnabled(true);
        view.getSettings().setLoadsImagesAutomatically(true);
        view.getSettings().setLoadWithOverviewMode(true);
        view.getSettings().setUseWideViewPort(true);
        view.getSettings().setDatabaseEnabled(true);
        view.getSettings().setDomStorageEnabled(true);
        view.setWebViewClient(new MyWebViewClient());

        view.loadUrl("http://58.182.126.214:4567");
    }

    class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        startActivity(intent);
        return true;
        }
    }

}