美文网首页
h5与app交互WebViewJavascriptBridge

h5与app交互WebViewJavascriptBridge

作者: 如果俞天阳会飞 | 来源:发表于2023-07-31 14:18 被阅读0次
// 注入全局JS对象
webView.addJavascriptInterface(new NativeBridge(this), "NativeBridge");

class NativeBridge {
  private Context ctx;
  NativeBridge(Context ctx) {
    this.ctx = ctx;
  }

  // 增加JS调用接口
  @JavascriptInterface
  public void showNativeDialog(String text) {
    new AlertDialog.Builder(ctx).setMessage(text).create().show();
  }
}

在Web端直接调用这个方法即可:

window.NativeBridge.showNativeDialog('hello');

之前一个案例

getLimit () { // 获取额度
      const that = this
      /* 调用APP 方法获取额度 */
      WebViewJavascriptBridge.callHandler(
        'postDeviceInfo'
        , {}
        , function (response) {
          that.spinnerShow = false
          const res = JSON.parse(response)
          if (res.code == 0) {
            // that.loanBtnShow = false;
            that.updateLimit(res.data)
          } else {
            that.$toast({
              msg: res + 'exception'
            })
          }
        }
      )
    },

相关文章

网友评论

      本文标题:h5与app交互WebViewJavascriptBridge

      本文链接:https://www.haomeiwen.com/subject/ejmepdtx.html