AppCan js到native 通信流程
通过js调用native需要做以下几步:
1.在plugin.xml配置文件里,添加如下内容
<?xml version="1.0" encoding="utf-8"?> <uexplugins> <plugin className="com.test.testObject" uexName="test"> <method name="setAction"/> </plugin> </uexplugins> |
2.添加com.test.testObject类,添加如下内容
package com.test; import org.zywx.wbpalmstar.engine.EBrowserView; import org.zywx.wbpalmstar.engine.universalex.EUExBase; import org.zywx.wbpalmstar.engine.universalex.EUExCallback; public class testObject extends EUExBase { private Context context; public testObject(Context context, EBrowserView inParent) { super(context, inParent); } @Override protected boolean clean() { return false; } public void setAction(String[] message){ //TODO jsCallback("test.setActionCallBack",0, EUExCallback.F_C_JSON,message[0]); } } |
setAction里面可以设定需要native做的操作,demo里直接设定了回调给js
3.js调用添加,在需要调用的页面添加js内容如下
function setAction(message){ test.setAction(message); } window.uexOnload = function(type){ if(type == 0){ test.setActionCallBack = function(opId,dataType,data){ //TODO alert(data); }; } } |
通过setAction进行调用,最终会把message以消息的方式弹出.
Categories: Android, javaScript, 前端开发, 移动开发