1***@163.com
1***@163.com
  • 发布:2021-12-13 10:07
  • 更新:2022-07-31 23:24
  • 阅读:2171

原生插件开发怎么在android代码中给uniapp端派发全局响应事件?

分类:uni-app

我在android代码中,百度搜到api说用fireGlobalEventCallback派发事件和传参到uniapp中,代码如下

iuhfService.setOnInventoryListener(new OnSpdInventoryListener() {  
                JSONObject mapObj = new JSONObject();  
                @Override  
                public void getInventoryData(SpdInventoryData spdInventoryData) {  
                    //TODO 盘点成功回调  

                    mapObj.put("code", "success");  
                    mapObj.put("tag", "inventory");  
                    mapObj.put("data", spdInventoryData);  
                    mWXSDKInstance.fireGlobalEventCallback("sendMessage", mapObj);  
                }  

                @Override  
                public void onInventoryStatus(int i) {  
                    //TODO 盘点失败回调  
                    mapObj.put("code", "fail");  
                    mapObj.put("tag", "inventory");  
                    mapObj.put("data", i);  
                    mWXSDKInstance.fireGlobalEventCallback("sendMessage", mapObj);  
                }  
            });

但是我在uniapp中注册了监听事件,android端我也断点debug了,mWXSDKInstance.fireGlobalEventCallback("sendMessage", mapObj)确实执行了,但是uniapp中的监听的注册时间并没有什么反应。官网中找不到有什么其它方式实现这种需求,求教
//注册全局变量用于接收后台程序主动发起的数据并且记录最后一次盘点的卡数据用来测试读卡
plus.globalEvent.addEventListener('sendMessage', function(e){
if(e.tag == "inventory"){
that.cardInfo = JSON.parse(e.data);
}else if(e.tag == "read"){
that.readInfo = JSON.parse(e.data);
}else if(e.tag = "write"){
that.writeInfo = JSON.parse(e.data);
}
modal.toast({
message: "sendMessage收到:"+e.data,
duration: 1.5
});

        });
2021-12-13 10:07 负责人:无 分享
已邀请:
师大酸梅杨

师大酸梅杨

试试

mWXSDKInstance.fireGlobalEventCallback(arg1,arg2)

不行的话,调试下

public void fireGlobalEventCallback(String eventName, Map<String, Object> params) {  
        List<String> callbacks = (List)this.mGlobalEvents.get(eventName);  
        if (callbacks != null) {  
            Iterator var4 = callbacks.iterator();  

            while(var4.hasNext()) {  
                String callback = (String)var4.next();  
                WXSDKManager.getInstance().callback(this.mInstanceId, callback, params, true);  
            }  
        }  

    }

this.mGlobalEvents 里面有你声明的function应该就会触发

1***@139.com

1***@139.com

android 代码:

@Override
public void onGetPoiResult(PoiResult poiResult) {

    if(null != mPoiSearch){  
        Log.i("onGetPoiResult","PoiSearch destroy");  
        mPoiSearch.destroy();  
    }  

    if (poiResult == null || poiResult.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {  
        Log.i("onGetPoiResult","onGetPoiResult get null");  
        return;  
    }  

    List<PoiInfo> poiInfos = poiResult.getAllPoi();  
    if (null == poiInfos) {  
        return;  
    }  

    Map<String,Object> params = new HashMap<>();  
    JSONArray list = new JSONArray();  

    if(poiInfos.size() > 0){  

        for(PoiInfo poiInfo:poiInfos){  
            JSONObject obj = new JSONObject();  
            String name = poiInfo.getName() == null|| poiInfo.getName()== "" ?poiInfo.getAddress():poiInfo.getName() ;  
            obj.put("name",name);  
            obj.put("address", poiInfo.getAddress());  
            obj.put("longitude",  poiInfo.getLocation().longitude);  
            obj.put("latitude",  poiInfo.getLocation().latitude);  
            obj.put("province",  poiInfo.getProvince());  
            obj.put("city",  poiInfo.getCity());  
            obj.put("area", poiInfo.getArea());  
            Log.i("onGetPoiResult","poiInfo: "+poiInfo.toString());  
            list.add(obj);  
        }  
    }  

    params.put("pois",list);  
    mUniSDKInstance.fireGlobalEventCallback("js_poi_get_result_callback", params);  
}  

JS代码:
const globalEvent = uni.requireNativePlugin('globalEvent');

const poiGetNearbyPoiCallback = "js_nearby_poi_get_result_callback";
globalEvent.addEventListener(poiGetNearbyPoiCallback, function (json) {
console.log(json);
});

  • l***@genars.net

    请问下js这个代码是写在onload里面吗

    2022-07-05 17:16

l***@genars.net

l***@genars.net

刚解决 uni代码onLoad() {
var that = this;
var globalEvent = uni.requireNativePlugin('globalEvent');
globalEvent.addEventListener('myEvent', function(e) {
console.log('myEvent'+JSON.stringify(e));
that.title = JSON.stringify(e);
});
},
安卓代码:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == REQUEST_CODE && data.hasExtra("respond")) {
Log.e("TestModule", "原生页面返回----"+data.getStringExtra("respond"));
dataString = data.getStringExtra("respond");
Map<String,Object> params=new HashMap<>();
params.put("key",dataString);
mUniSDKInstance.fireGlobalEventCallback("myEvent", params);
} else {
Log.e("TestModule", "原生页面返回2----"+data.getStringExtra("respond"));
}
}

aak12345

aak12345

List<WXSDKInstance> instances = WXSDKManager.getInstance().getWXRenderManager().getAllInstances();
for (WXSDKInstance instance : instances) {
instance.fireGlobalEventCallback(eventName, result);
}

该问题目前已经被锁定, 无法添加新回复