设为首页 加入收藏

TOP

Hybrid APP基础篇(五)->JSBridge实现示例(三)
2017-10-11 15:24:32 】 浏览:8352
Tags:Hybrid APP 基础 -> JSBridge 实现 示例
})(); //注册一个测试函数 JSBridge.registerHandler('testH5Func', function(data, callback) { alert('测试函数接收到数据:' + JSON.stringify(data)); callback && callback('测试回传数据...'); }); /* ***************************API******************************************** * 开放给外界调用的api * */ window.jsapi = {}; /** ***app 模块 * 一些特殊操作 */ jsapi.app = { /** * @description 测试函数 */ testNativeFunc: function() { //调用一个测试函数 JSBridge.callHandler('testNativeFunc', {}, function(res) { callback && callback(res); }); } }; })();

Android实现部分

说明

这是Android原生中配套的JSBridge实现代码。Android的实现相对比JS复杂,包括多个部分

JSBridge类实现

实现代码如下

public class JSBridge {
    private static Map<String, HashMap<String, Method>> exposedMethods = new HashMap<>();
	
	//原生注册API方法
    public static void register(String exposedName, Class<? extends IBridge> clazz) {
        if (!exposedMethods.containsKey(exposedName)) {
            try {
                exposedMethods.put(exposedName, getAllMethod(clazz));
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
	
	//得到所有的注册方法
    private static HashMap<String, Method> getAllMethod(Class injectedCls) throws Exception {
        HashMap<String, Method> mMethodsMap = new HashMap<>();
        Method[] methods = injectedCls.getDeclaredMethods();
        for (Method method : methods) {
            String name;
            if (method.getModifiers() != (Modifier.PUBLIC | Modifier.STATIC) || (name = method.getName()) == null) {
                continue;
            }
            Class[] parameters = method.getParameterTypes();
            if (null != parameters && parameters.length == 4) {
                if (parameters[0] == BaseWebLoader.class && parameters[1] == WebView.class && parameters[2] == JSONObject.class && parameters[3] == Callback.class) {
                    mMethodsMap.put(name, method);
                }
            }
        }
        return mMethodsMap;
    }

	//调用Hava中的方法
	//其中BaseWebLoader是JSBridge的webview容器(二次封装)
	//执行完方法后,如果又回到,自动就会调用
    public static String callJava(BaseWebLoader webLoader,WebView webView, String uriString) {
        String methodName = "";
        String className = "";
        String param = "{}";
        String port = "";
        if (!TextUtils.isEmpty(uriString) && uriString.startsWith("EpointJSBridge")) {
            Uri uri = Uri.parse(uriString);
            className = uri.getHost();
            param = uri.getQuery();
            port = uri.getPort() + "";
            String path = uri.getPath();
            if (!TextUtils.isEmpty(path)) {
                methodName = path.replace("/", "");
            }
        }


        if (exposedMethods.containsKey(className)) {
            HashMap<String, Method> methodHashMap = exposedMethods.get(className);

            if (methodHashMap != null && methodHashMap.size() != 0 && methodHashMap.containsKey(methodName)) {
                Method method = methodHashMap.get(methodName);
                if (method != null) {
                    try {
                        method.invoke(null,webLoader, webView, new JSONObject(param), new Callback(webView, port));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return null;
    }
}	
			

这个类的作用是原生定义一些暴露的api

Callback类实现

实现代码如下

public class Callback {
    private static Handler mHandler = new Handler(Looper.getMainLooper());
    private static final String CALLBACK_JS_FORMAT = "java script:JSBridge._handleMe
首页 上一页 1 2 3 4 5 6 7 下一页 尾页 3/8/8
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇全球首个实战类微信小程序开发教程 下一篇微信小程序组件scroll-view

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目