JSBridge
Android WebView JavaScript Bridge. Inspired by marcuswestin's WebViewJavascriptBridge https://github.com/marcuswestin/WebViewJavascriptBridge
Install / Use
/learn @wangdahoo/JSBridgeREADME
Android WebViewJavascriptBridge
gradle script for jsbridge.jar
./gradlew :jsbridge:publish
生成的jar包和js文件位于jsbridge/dist/目录下
how to use
- 复制jsbridge/dist/WebViewJavascriptBridge.js到应用目录的assets文件夹下,复制jsbridge/dist/js-bridge.jar到应用目录的libs目录下;
- 在应用的build.gradle文件中添加依赖
dependencies {
...
compile files('libs/jsbridge-1.0.0.jar')
}
- Sample Code
// Code In Activity:
import com.wangdahoo.jsbridge.JSBridgeWebView;
import com.wangdahoo.jsbridge.JSBridgeWebViewClient;
import com.wangdahoo.jsbridge.MessageDispatcher;
...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 1. 创建JSBridgeWebView实例
webView = new JSBridgeWebView(this);
// 2. 创建MessageDispather:消息派发组件, 拦截js端发送过来的消息,然后派发给消息中指定的消息处理组件(Handler)进行处理
MessageDispatcher messageDispatcher = new MessageDispatcher();
// 3. 给MessageDispather添加Hanlder
messageDispatcher.registerHandler("DialogAlert", new DialogAlertHandler(this));
// 4. 设置WebViewClient
webView.setWebViewClient(new JSBridgeWebViewClient(webView, messageDispatcher));
...
}
...
// 实现MessageHanlder
public class DialogAlertHandler extends BaseMessageHandler implements MessageHandler {
String title = "提示";
String content = "";
String btnOkText = "确定";
Context context;
public DialogAlertHandler(Context context) {
this.context = context;
}
// 实现handle方法即可
@Override
public void handle(JSONObject message, final Callback callback) {
JSONObject data = getData(message);
final String callbackId = getStringValueByField(message, "callbackId");
if (data.has("title"))
title = getStringValueByField(data, "title");
if (data.has("content"))
content = getStringValueByField(data, "content");
if (data.has("btnOkText"))
btnOkText = getStringValueByField(data, "btnOkText");
AlertDialog.Builder builder = new AlertDialog.Builder(context)
.setTitle(title)
.setMessage(content)
.setPositiveButton(btnOkText, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (callback != null)
callback.onCallback(makeResponse(callbackId));
}
});
builder.create();
builder.show();
}
}
That's all and have fun.
Related Skills
node-connect
339.1kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
frontend-design
83.8kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
339.1kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
commit-push-pr
83.8kCommit, push, and open a PR
