码农曾的博客

正在加载中......

Android_Android&H5的混合交互基础

最近新接手的项目几天熟悉下来发现原生的页面没几个,基本都是混合开发调用的H5,于是学习下android&H5的交互,方便日后维护和开发

交互都是在webview中进行,所以要让weiview先支持js:

  webViewSettings.setJavaScriptEnabled(true);

一.JS调Android

1.1 和H5开发人员沟通好调用方法名:mzyApp

        

2.2H5伪代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title></title>
</head>
<body>

<button id="btn0">调用android无参参数</button>
<button id="btn1">调用android有参参数</button>

<a href='aa://atguigu/path'>点我试试</a>
<a href='https://www.baidu.com'>百度</a>

<div id="content"></div>

</body>

<script type="text/javascript">
        var name = "啊福老师 哇哇哇"
        document.getElementById("btn0").onclick = function(){
//android是传过来的对象名称,mzysApp是android中的方法
            android.mzysApp();
        };
        document.getElementById("btn1").onclick = function(){
//android是传过来的对象名称,mzysApp是android中的方法
                android.mzysApp(name);
        };

        var content = document.getElementById("content");

        function message(){
            content.innerHTML = "调用了有参的js函数"
        };

        function message2(des){
            content.innerHTML = "调用了"+des;
        };
    </script>
</html>
2.3.Android伪代码:
 @JavascriptInterface
    public void mzyxApp(String json){
        mfragment.mzyxApp(json);
        TLog.log("Html Json:", json);
    }
2.4 Android逻辑处理(对照交互文档的code)

 public void mzyxApp(String json){
        MainActivity activity = (MainActivity) _mActivity;
        ActionAndMethodTypeBean type = JsonUtil.getType(json);
        if(type == null){
            return;
        }
        switch (type.actionType){
            case Constant.ACTION_NO_PARAM:
                goActionNoParamPage(json);
                break;
            case Constant.ACTION_PARAM:
                goActionParamPage(json);
                break;
            case Constant.ACTION_ERROR_TOKEN:
                activity.exitAndGoLogin();
                break;
            case Constant.ACTION_LOAD_START:
                _mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogHelper.showDialogForLoading(_mActivity,"加载中...",false);
                    }
                });
                break;
            case Constant.ACTION_LOAD_STOP:
                _mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        DialogHelper.stopProgressDlg();
                    }
                });
                break;
            case Constant.ACTION_CLOSE_KEY_BOARD:
                activity.closeSearchBtn();
                break;
            case Constant.ACTION_CLEAN_CHCHE:
                doCleanCache();
                break;
            case Constant.ACTION_SHARE:
                doShare(json);
                break;
            case Constant.ACTION_CLOSE:
                _mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        _mActivity.onBackPressed();
                    }
                });
                break;
            case Constant.ACTION_CLOSE_AND_REFRESH:
                _mActivity.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        _mActivity.onBackPressed();
                    }
                });
                AppContext.getInstance().isCLoseAndRefresh = true;
                break;
            case Constant.ACTION_CHANGE_PWD:
                doChangePwd();
                break;
            case Constant.ACTION_EXIT:
                doExit();
                break;
            case Constant.ACTION_SEARCH:
                doSearch();
                break;
            case Constant.ACTION_HTML_NET_ERROR:
                AppToast.showShortText(this._mActivity, "网络错误,请重新加载");
                break;
            case Constant.ACTION_HTML_UNKNOWN_ERROR:
                if(!TextUtils.isEmpty(type.description)){
                    if(type.description.contains("Exception") | type.description.contains("exception") | type.description.contains("error")){
                        AppToast.showShortText(this._mActivity, "未知错误");
                    }else {
                        AppToast.showShortText(this._mActivity, type.description);
                    }
                }
                break;
            case Constant.ACTION_VERSION_UP:
                doDownload();
                break;
            case Constant.ACTION_SELECT_DATE:
//                doSelectDate();     //android   原生完成了这一部分功能
                break;
            case Constant.ACTION_SELECT_SINGLE_DATE:
                doSelectDate();
                break;
            case Constant.ACTION_SHOW_MSG:
                doOnlyAlertMsg(type.description);
                break;
            case Constant.ACTION_REFRESH_MONTH:
                changeMonth(json);
                break;
            case Constant.ACTION_MSG_CLIPBOARD:
                sendMsgToClipboard(json);
                break;
        }
    }

二.Android调JS

//这个是button的点击事件
 @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn1:
                //参数 “javascript:”  +  js方法名
                wv.loadUrl("javascript:message()");
                break;
            case R.id.btn2:
                //在android调用js有参的函数的时候参数要加单引号
                wv.loadUrl("javascript:message2('" + name + "')");
                break;
        }
    }

三.Cookie设置

通常我们在登录时获取到用户的Cookie信息,然后将其保存到sdcard的WebView缓存文件当中,这样在加载网页时,WebView会自动将当前url的本地Cookie信息放在http请求的request中,传递给服务器

    public void syncCookie(Context context) {
        CookieSyncManager.createInstance(context);
        CookieManager cookieManager = CookieManager.getInstance();
        cookieManager.setAcceptCookie(true);
        cookieManager.removeSessionCookie();//移除
        if (AppContext.getInstance().getUser() != null) {
            cookieManager.setCookie(mUrl, "leaderId=" + AppContext.getInstance().getUser().leaderId);
            cookieManager.setCookie(mUrl, "drugUserId=" + AppContext.getInstance().getId());
            cookieManager.setCookie(mUrl, "token=" + AppContext.getInstance().getToken());
            cookieManager.setCookie(mUrl, "userType=" + AppContext.getInstance().getUserType());
            cookieManager.setCookie(mUrl, "telephone=" + AppContext.getInstance().getUser().telephone);
            cookieManager.setCookie(mUrl, "email=" + AppContext.getInstance().getUser().email);
        }
        if (AppContext.getInstance().getUserLookLimit() != null) {
            if (AppContext.getInstance().getUserLookLimit().contains(Constant.LIMIT_READ_BEST)) {
                cookieManager.setCookie(mUrl, "practice=1");
            } else {
                cookieManager.setCookie(mUrl, "practice=0");
            }
        }
        CookieSyncManager.getInstance().sync();
    }


        

阅读更多
想对作者说点什么? 我来说一句

纯电动及混合动力汽车设计基础

2016年07月01日 43.35MB 下载

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭