如何在Android 4.2和4.1中处理WebView

时间:2016-01-06 07:06:43

标签: android webview webviewclient sslerrorhandler

我的Webview适用于android 4.4.4及更高版本。如何在所有Android设备中处理它?! 我的意思是我尝试了很多方法在Webview中显示PaymentGateWay,但它只是在webview页面中显示了url地址(Cant Load Url)。我可以用这段代码来做这些吗?

    WebSettings webSetting = wv.getSettings();
    webSetting.setJavaScriptEnabled(true);
    webSetting.setJavaScriptCanOpenWindowsAutomatically(true);
    final JavaScriptInterface JavaScriptInterface = new JavaScriptInterface(WebViewPaymentByBankActivity.this);
    wv.addJavascriptInterface(JavaScriptInterface, "Android");
    //webView.setWebChromeClient(new MyWebChromeClient());
    //webView.getSettings().setSupportMultipleWindows(true);
    //webSetting.setPluginState(WebSettings.PluginState.ON);
    //webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
    //webSetting.setDomStorageEnabled(true);
    webSetting.setCacheMode(WebSettings.LOAD_NO_CACHE);
    webSetting.setAppCacheEnabled(true);

    wv.setWebChromeClient(new WebChromeClient() {

        public boolean onJsAlert(WebView view, String url, String message, final android.webkit.JsResult result) {
            //Toast.makeText(WebViewPaymentByBankActivity.this, message, Toast.LENGTH_LONG).show();
            result.confirm();
            return true;
        }

        public void onProgressChanged(WebView view, int progress) {

            activity.setProgress(progress * 1000);
            if (progress == 100) {
            }
        }
    });

    wv.setWebViewClient(new WebViewClient() {
        @JavascriptInterface
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String urlNewString) {

            if (!loadingFinished) {
                redirect = true;
            }
            loadingFinished = false;
            wv.loadUrl(urlNewString);
            url_result = urlNewString;
            uri = Uri.parse(url_result);
            String[] result = url_result.split("://");
            protocol = result[0];
            host = result[1];

            if (protocol.equals("inapp")) {
                if (host.equals("Error")) {
                    Intent intent = new Intent(WebViewPaymentByBankActivity.this,PaymentActivity.class);
                    startActivity(intent);
                    WebViewPaymentByBankActivity.this.finish();
                }
                else if (host.equals("Success")) {
                    Constants.ck_pay=true;
                    BasketFragment.sum = 0;
                    BasketFragment.txt_view_badge.setText("");
                    BasketFragment.txt_view_badge.setVisibility(View.GONE);
                    PaymentActivity.txt_view_pay_total.setText("0");
                    PaymentActivity.txt_view_pay_payable.setText("0");
                    WebViewPaymentByBankActivity.this.finish();
                }
            }
            return false;
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if (!redirect) {
                loadingFinished = true;
            }

            if (loadingFinished && !redirect) {
                //HIDE LOADING IT HAS FINISHED
            } else {
                redirect = false;
            }
            super.onPageFinished(view, url);
            view.clearCache(true);
        }

        @Override
        @JavascriptInterface
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            handler.proceed();
        }
    });
}


public class JavaScriptInterface {
    Context mContext;

    /**
     * Instantiate the interface and set the context
     */
    JavaScriptInterface(Context c) {
        mContext = c;
    }

    /**
     * Show a toast from the web page
     */
    @JavascriptInterface
    public void paymentResponse(String toast) {
        Toast.makeText(mContext, toast, Toast.LENGTH_SHORT).show();
    }
}


    @Override
    @JavascriptInterface
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK)) {
            wv.goBack();
            Intent intent = new Intent(WebViewPaymentByBankActivity.this, PaymentActivity.class);
            startActivity(intent);
            WebViewPaymentByBankActivity.this.finish();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
        Intent intent = new Intent(WebViewPaymentByBankActivity.this, PaymentActivity.class);
        startActivity(intent);
        WebViewPaymentByBankActivity.this.finish();
        return super.onKeyDown(keyCode, event);
    }

0 个答案:

没有答案