GithubHelp home page GithubHelp logo

huailong / jswebview Goto Github PK

View Code? Open in Web Editor NEW

This project forked from tamicer/jswebview

0.0 2.0 0.0 135 KB

基于JsBridge封装的高效自带加载进度条的WebView

License: Apache License 2.0

Java 83.72% HTML 8.16% JavaScript 8.12%

jswebview's Introduction

JsWebView

Android基于JsBridge封装的高效带加载进度的WebView 可用作简单应用内置浏览器,帮你快速开发Hybrid APP

主要功能:

  • 支持header
  • 支持进度
  • 支持自定义错误页面
  • 支持h5和native的快速交互,简单易学
  • 支持cookie同步

#UI

#Dependencies

Gradle:

root:

  repositories {
maven { url "https://jitpack.io" }
jcenter()
  }

Module:

   dependencies {
   .....
   compile 'com.tamic:browse:1.0.0'

   }

Function

   XMl

 <com.tamic.jswebview.view.ProgressBarWebView
    android:id="@+id/login_progress_webview"
    style="@style/NumberProgressBar_Default"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

 </com.tamic.jswebview.view.ProgressBarWebView>

初始化

    ProgressBarWebView  mProgressBarWebView = (ProgressBarWebView) findViewById(R.id.login_progress_webview);

设置WebViewClient

      mProgressBarWebView.setWebViewClient(new CustomWebViewClient(mProgressBarWebView.getWebView()) {
        @Override
        public String onPageError(String url) {
            //指定网络加载失败时的错误页面
            return "file:///android_asset/error.html";
        }

        @Override
        public Map<String, String> onPageHeaders(String url) {

            // 可以加入header

            return null;
        }

        
    });

    // 打开页面,也可以支持网络url
    mProgressBarWebView.loadUrl("file:///android_asset/demo.html");

回调js的方法

    // 添加hander方法名 
    mHandlers.add("login");
   // 订阅此方法key
    mProgressBarWebView.registerHandlers(mHandlers, new JsHandler() {
        @Override
        public void OnHandler(String handlerName, String responseData, CallBackFunction function) {

                String resquestData = "this native data"
                
                 // 返回数据给js
                function.onCallBack(resquestData);
           
        }
    });

调用js

    mProgressBarWebView.callHandler("callNative", "hello H5, 我是java", new JavaCallHandler() {
        @Override
        public void OnHandler(String handlerName, String jsResponseData) {
            Toast.makeText(MainActivity.this, "h5返回的数据:" + jsResponseData, Toast.LENGTH_SHORT).show();
        }
    });

发送消息给js

    mProgressBarWebView.send("hello world!", new CallBackFunction() {
        @Override
        public void onCallBack(String data) {
        
        // data 为js回传数据

            Toast.makeText(MainActivity.this, data, Toast.LENGTH_SHORT).show();

        }
    });

作者:Tamic & Yezhengnan.

介绍:http://www.jianshu.com/p/17054c21dd78

 >http://blog.csdn.net/sk719887916/article/details/52402470

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.