在webview中Android打开新活动不成功 - shouldOverrideUrlLoading无效

时间:2016-08-18 04:38:42

标签: android android-webview

我想做一个主要基于webview的应用程序。 MainActivity是加载www.example.com/products.php。

www.example.com/products.php - 显示所有产品*

www.example.com/products.php?id=123 - 根据产品ID显示单个产品

www.example.com/cart.php - 点击"添加到购物车"在product.php?id = 123上,它不会打开新活动,但会保留在同一活动中。

MainActivity.java

    private WebView wv;

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

    wv = (WebView) findViewById(R.id.webView);
    WebSettings wss = wv.getSettings();
    wss.setJavaScriptEnabled(true);
    wv.loadUrl("http://www.example.com/product.php");
    wv.setWebViewClient(new WebViewClient());

}
private class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("www.example.com/cart.php")) {
            // This is my web site, so do not override; let my WebView load the page
            return false;
        }else{
            // 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;

        }

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

还有#34; shouldOverrideUrlLoading" ,它显示折旧...那么它的新名称是什么?

0 个答案:

没有答案