Giter VIP home page Giter VIP logo

jsbridge's Introduction

功能

简单JSBridge,使用JSBridge实现android端与web端安全通信。

使用

Gradle

compile 'cn.opensrc.brilib:jsbridge:1.0.0'

Maven

<dependency>
  <groupId>cn.opensrc.brilib</groupId>
  <artifactId>jsbridge</artifactId>
  <version>1.0.0</version>
  <type>pom</type>
</dependency>

lvy

<dependency org='cn.opensrc.brilib' name='jsbridge' rev='1.0.0'>
  <artifact name='jsbridge' ext='pom' ></artifact>
</dependency>

代码示例

Js调用Java

// 可以在 shouldOverrideUrlLoading() 或 onJsPrompt()方法或其它回调方法中执行
// web 端传来的js消息格式:jsbridge://className/methodName?{"param1":"value1"}
wv.loadUrl("file:///android_asset/test.html");
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebChromeClient(new WebChromeClient(){
    @Override
    public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {

        result.confirm("");
        Uri uri = Uri.parse(message);

        String className = JSBridge.getClassName(uri);
        String methodName = JSBridge.getMethodName(uri);
        JSONObject JSONParams = JSBridge.getMethodJsonParams(uri);

        JSBridge.register(className,BridgeImp.class);

        if (JSONParams == null){
            JSBridge.callJavaMethod(className,methodName);
        }else {
            if ("showToast".equals(methodName))
                JSBridge.callJavaMethod(className,methodName,MainAty.this,JSONParams.optString("msg"));
            else if ("log".equals(methodName))
                JSBridge.callJavaMethod(className,methodName,JSONParams.optString("msg"));
        }

        return true;
    }
});

Java调用Js

// 参数1为WebView
// 参数2为需要调用的js函数名
// 参数3为可变参,为js函数需要的参数,无参数可不传入即可
JSBridge.callJSFunc(wv,"func1","sharp");

使用注意

  1. API >= 11 能使用本库.
  2. web端传到Android端的消息格式为:jsbridge://className/methodName?{"param1":"value1"}
  3. web端调用android端方法时,暴露的java类请先调用JSBridge.register方法进行注册。
  4. 具体使用请参见JSBridge类。

联系方式

jsbridge's People

Stargazers

Jiakuo Liu avatar j0k3r avatar  avatar

Watchers

Neo Chen avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.