agentWeb JS 交互部分, Android 端的集成 和具体使用方法

1.集成

implementation 'com.just.agentweb:agentweb:4.1.2'

2.布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
<LnearLayout
    android:id="@+id/web"
    android:scrollbars="none"
    android:orientation="vertical"
    android:layout_below="@+id/ly_title"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
</RelativeLayout>

3.Java代码

private LinearLayout weblayout;
private AgentWeb agentWeb;
weblayout = findViewById(R.id.web);
agentWeb = AgentWeb.with(this)
        .setAgentWebParent((LinearLayout) weblayout, new LinearLayout.LayoutParams(-1, -1))//在父布局文件内找到web
        .useDefaultIndicator()
        .createAgentWeb()
        .ready()
        .go(url);

//允许js互调(android 固定值,new AndroidInterface 自己定义的接口)
agentWeb.getJsInterfaceHolder().addJavaObject("android", new AndroidInterface(agentWeb, this, this));
h5  调android:

AndroidInterface  写法如下:
public class AndroidInterface {
    private AgentWeb agent;
    private Context context;
    private WebJsInterfaceCallback interfaceCallback;
    private String TAG = "AndroidInterfaceWeb";
    public AndroidInterface(AgentWeb agent, Context context, WebJsInterfaceCallback interfaceCallback) {
        this.agent = agent;
        this.context = context;
        this.interfaceCallback = interfaceCallback;
    }


//定义h5要调用的本地方法
@JavascriptInterface
public void Android_BuyVip(String methodname, String parms) {
    switch (methodname) {
//回调方法名
        case "tobuy":
            if (interfaceCallback != null) {
                interfaceCallback.tobuy(parms);
            }
            break;
    }
}
/回調函数
public interface WebJsInterfaceCallback {

//获取h5传递的数据的方法回调
    void tobuy(String parms);

}
}

页面实现AndroidInterface.WebJsInterfaceCallback

public class Activity implements AndroidInterface.WebJsInterfaceCallback {

@Override public void tobuy(String string) {
//string h5传来的参数
}
}

 

android  调h5

agentWeb.getJsAccessEntrace().quickCallJs("androidpay");//androidpay()h5里的方法

 

有不懂的直接留言就行

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值