Android的webview操作程序员

android js bridge 互相调用第二版

2018-05-04  本文已影响0人  leewug

android-js-bridge-v2

android js 互相调用 第二版

1. 支持js匿名函数接收
2. 支持js json对象接收
3. 支持js函数返回值获取
4. 通过注解注入js方法
5. 优化第一版的反射注入方式,采用注解处理器编译时生成注入代码,提高运行效率
6. 加入简单的 webview 预加载功能

https://github.com/lwugang/android-js-bridge-v2

Add it in your root build.gradle at the end of repositories:

    allprojects {
        repositories {
            ...
            maven { url 'https://jitpack.io' }
        }
    }

Add the dependency

  dependencies {
      implementation 'com.github.lwugang.android-js-bridge-v2:library:v2.0'
        implementation 'com.github.lwugang.android-js-bridge-v2:js-bridge-anno:v2.0'
        annotationProcessor 'com.github.lwugang.android-js-bridge-v2:js-bridge-compiler:v2.0'
    }

使用方式

    <com.wugang.jsbridge.library.BridgeWebView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/web_view"/>

Activity

    public class MainActivity extends AppCompatActivity {

       @Override protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          PreLoadManager.get(this).preload("http://www.baidu.com", "http://www.youku.com");
          BridgeWebView webView = (BridgeWebView) findViewById(R.id.web_view);
          webView.getJsBridge().register("AB",new AB(this));
          webView.loadUrl("file:///android_asset/test.html");
          WebView.setWebContentsDebuggingEnabled(true);
       }

      @JsInject public static class AB extends JsPlugin {
          private Context context;

          public AB() {
          }

          public AB(Context context) {
            this.context = context;
          }

          public void test(String s, JSFunction jsFunction) {
            Toast.makeText(context, "js调用我", 1).show();
            jsFunction.execute("test execute   " + s);
          }

          public void test1() {
            Log.e("-------", "test1: ");
          }

          public void test2() {
            Log.e("-------", "test2: ");
          }
      }
    }

HTML&JS代码

<html>
<script>
      // 推荐使用方式,否则直接调用将无法调用到 原生方法
      window.JSBridgeReady=function(){
          console.log("---window EasyJSReady---")
          AB.test2();
          AB.test("call test",function(ret){
              console.log(ret)
          })
      }
</script>
<script src="test.js"></script>

<body>
  <button onclick="javascript:location.reload()">refresh</button>
</body>
</html>

网页预加载

   //预加载,推荐在Application中调用
   PreLoadManager.get(this).preload("http://www.baidu.com", "http://www.youku.com");
Activity 中使用
public class PreLoadActivity extends AppCompatActivity {
  @Override protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    BridgeWebView webView = PreLoadManager.get(this).getWebView();
    setContentView(webView);
    webView.setWebViewClient(new WebViewClient(){
      @Override public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        if(request.hasGesture()){
          Intent intent = new Intent(PreLoadActivity.this,PreLoadActivity.class);
          intent.putExtra("url",request.getUrl());
          startActivity(intent);
          return true;
        }
        return super.shouldOverrideUrlLoading(view, request);
      }
    });
    webView.loadUrl(getIntent().getStringExtra("url"));
  }
}

参考项目https://github.com/lwugang/safe-java-js-webview-bridge

参考项目https://github.com/dukeland/EasyJSWebView

android-js-bridge-v2p.gif
上一篇下一篇

猜你喜欢

热点阅读