1. 程式人生 > >android和h5頁面互動

android和h5頁面互動

專案h5頁面那邊需要調native頁面的充值和提現頁面

設定支援js


        mWebView = view.findViewById(R.id.mall_view);
        settings = mWebView.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setLoadsImagesAutomatically(true);
        settings.setDomStorageEnabled(true);
        //不快取
        settings.setCacheMode(WebSettings.LOAD_NO_CACHE);

內部建立一個類

    /**
     * 商城調充值頁面
     */
    class JsRecharge {
        @JavascriptInterface
        public void chargemoney() {
           //跳轉充值頁面
            Intent intent = new Intent(getActivity(), MoneyRechargeActivity.class);
            startActivity(intent);
        }
    }
    /**
     * 商城調提現頁面
     */
    class JsPutForward {
        @JavascriptInterface
        public void putforward() {
           //跳轉提現頁面
            Intent intent = new Intent(getActivity(), MoneyRefillActivity.class);
            startActivity(intent);
        }
    }

webview呼叫

        //充值 引數1:自定義的類物件  2:自己隨便起的名字,需要將此名字告訴h5那邊
      mWebView.addJavascriptInterface(new JsRecharge(), "recharge");
        //提現
      mWebView.addJavascriptInterface(new JsPutForward(), "withdraw");

//js呼叫:windows.recharge.chargemoney;windows.withdraw.putforward;即可
//windows應該相當於js那邊上下文的之類的;recharge是自己隨便起的那個名字;chargemoney是自定義類裡面寫的那個方法名