Android AgentWeb Android和Js交互

JavaScript07

Android AgentWeb Android和Js交互,第1张

1、Android 调用js

mAgentWeb.getJsAccessEntrace().quickCallJs(" ")

quickCallJs("方法名",参数...);

方法名不带 ()

例:

js方法   getJs()

mAgentWeb.getJsAccessEntrace().quickCallJs("getJs ")

js方法   getJs(var a,var b)

mAgentWeb.getJsAccessEntrace().quickCallJs("getJs ",a,b) 

2、js调用Android

mAgentWeb.getJsInterfaceHolder().addJavaObject("android",new Class())

js调用 window.android.方法名(参数)

Android中提供的方法 需要添加注解  @JavascriptInterface

Android中可以使用WebView加载网页,同时Android端的java代码可以与网页上的javascript代码之间相互调用。

一 Android部分:

布局代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:focusable="true" android:focusableInTouchMode="true" android:orientation="vertical" android:padding="8dp" tools:context=".MainActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <EditText android:id="@+id/input_et" android:layout_width="0dp" android:layout_height="wrap_content" android:singleLine="true" android:layout_weight="1" android:hint="请输入信息" /> <Button android:text="Java调用JS" android:layout_width="wrap_content" android:layout_height="wrap_content" android:onClick="sendInfoToJs" /></LinearLayout> <WebView android:id="@+id/webView" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

Activity代码:

public class MainActivity extends AppCompatActivity { private WebView webView @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) webView = (WebView) findViewById(R.id.webView) webView.setVerticalScrollbarOverlay(true) //设置WebView支持JavaScript webView.getSettings().setJavaScriptEnabled(true) webView.loadUrl("file:///android_asset/webview.html") //在js中调用本地java方法 webView.addJavascriptInterface(new JsInterface(this), "AndroidWebView") ////添加客户端支持 webView.setWebChromeClient(new WebChromeClient()) } private class JsInterface { private Context mContext public JsInterface(Context context) { this.mContext = context } //在js中调用window.AndroidWebView.showInfoFromJs(name),便会触发此方法。 @JavascriptInterface public void showInfoFromJs(String name) { Toast.makeText(mContext, name, Toast.LENGTH_SHORT).show() } } //在java中调用js代码 public void sendInfoToJs(View view) { String msg = ((EditText) findViewById(R.id.input_et)).getText().toString() //调用js中的函数:showInfoFromJava(msg) webView.loadUrl("javascript:showInfoFromJava('" + msg + "')")//webView.loadUrl("javascript:showInfoFromJava()") } }

二 网页代码

<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <title>Android WebView 与 Javascript 交互</title> </head><body><input type="button" value="分享" onclick="f1()"> <input type="text" id="show"/> </body><script> function f1(){ AndroidWebView.showInfoFromJs("hello")} function showInfoFromJava(msg){ document.getElementById("show").value=msgalert(1) }</script></html>

注意: android 调用js代码可能会报错如下:

W/WebView(2088): java.lang.Throwable: A WebView method was called on thread 'JavaBridge'. All WebView methods must be called on the same thread.

解决办法:

webView.post(new Runnable() { @Override public void run() { webView.loadUrl("javascript:showInfoFromJava('" + msg + "')")}})

移动开发中,我们经常会遇到混合开发模式,经常是在移动客户端中增加网页,来减少客户端的压力,同时也让软件更加灵活。废话少说,下面直接进入主题。

webView.getSettings().setAppCachePath(getCacheDir().getAbsolutePath())

webView.getSettings().setAppCacheEnabled(true)

if (Constant.DEBUG) {

webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE)

}

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

webView.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW)

}

//参数1 定义Android 与js交互的接口, 参数2 与js交互的一个常量,可以是任意一个常量字符串,

但是h5网页调用的时候需要增加这个参数二:handler

public class JSInterface{

@JavascriptInterface//这个标注必须加上 表示js要调用的方法,可以自动识别

public void getShareInfo(String strings) {

//todo android dosomething

}

}

那么 js端该如何调用这个方法呢\

android 和ios的调用方法不一样 因此 h5里面需要区别andorid和ios机型来分别调用移动端方法

如android:

//handler 是与android 约定的常量(最好做成全局通用的常量)

//getShareInfo 是与android 定义的调用方法

以上常量、方法、参数类型 必须一致才能成功调用

WebView提供两个事件回调类给应用层,分别为WebViewClient,WebChromeClient开发者可以继承

这两个类,接手相应事件处理。WebViewClient 主要提供网页加载各个阶段的通知,比如网页开始

加载onPageStarted,网页结束加载onPageFinished等;WebChromeClient主要提供网页加载过程

中提供的数据内容,比如返回网页的title,favicon等。

有需要监听加载网页进度

//需要webBar的可以自定义增加一个webBar

最后,就是大家都知道的webView.loadUrl(url)//url需要加载的网页

那么,如何在android调用js的方法呢

//getMsg() 为js里面暴露的方法

在销毁的时候:

protected void onDestroy() {\

super.onDestroy()\

if (webView != null) {\

webView.loadUrl("about:blank")\

webView.destory()\

}\

}