AgentWeb-Android-H5混合开发

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/cuizehui123/article/details/80071760

简介

agentweb 是对webview进行的又一层封装较为轻量级
所以基本的开发流程大致和webview原理相似
将html5文件方入asset文件夹下,访问路径为

    final private String CoachFile = "file:///android_asset/teacher/info-teacher.html";

运行demo

此demo使用了bintray/Jcenter 这个东西
Jcenter:看这个删除相关部分
[https://blog.csdn.net/u013231041/article/details/70174354]
需要在gradle 中将相关代码全部注释掉或者升级对应gradle 版本才能运行

原生webview-js使用

[https://blog.csdn.net/carson_ho/article/details/64904691]

[https://blog.csdn.net/carson_ho/article/details/52693322]

使用过程

  1. 集成
  2. JS-调android
  3. Android 调 js
    官网给出的代码片段

Android 调js

  function callByAndroid(){
      console.log("callByAndroid")
  }
  //此处为agentweb声明js方法
mAgentWeb.getJsAccessEntrace().quickCallJs("callByAndroid");

js调android

//可理解为agentweb注册interface
mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new AndroidInterface(mAgentWeb,this));
window.android.callAndroid();

AndroidInterface

 public class AndroidInterface {

    private Handler deliver = new Handler(Looper.getMainLooper());
    private AgentWeb agent;
    private Context context;

    public AndroidInterface(AgentWeb agent, Context context) {
        this.agent = agent;
        this.context = context;
    }
    //必须声明此注解

    @JavascriptInterface
    public String getToken(final String msg) {

        String accessToken=Config.getAccessToken(context);
        Log.i("Info", "Thread:" + Thread.currentThread());
        return accessToken;
    }

    @JavascriptInterface
    public int getID(){
        int id=Config.getUid(context);
        Log.d("uid:",""+id);
        return id;
    }
}

html调用部分片段

    getLocalData:function(){

                  if(window.android!=null&&typeof(window.android)!="undefined"){
                      id=window.android.getID();
                     alert(" : "+id);
                  }else{
                     alert(typeof(window.android));
                  }



            },

对框架的二次封装

-

猜你喜欢

转载自blog.csdn.net/cuizehui123/article/details/80071760