agentWeb Android 端的集成 和具体使用方法

tech2022-09-10  122

 

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里的方法

注:具体项目中使用出现白屏现象,查看demo没有找到问题  后来发现更换根布局为 RelativeLayout 白屏现象消失,后续查找具体原因.

最新回复(0)