我在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
});
});
4 个回复
师大酸梅杨
试试
不行的话,调试下
this.mGlobalEvents 里面有你声明的function应该就会触发
1***@139.com
android 代码:
@Override
public void onGetPoiResult(PoiResult poiResult) {
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
刚解决 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
List<WXSDKInstance> instances = WXSDKManager.getInstance().getWXRenderManager().getAllInstances();
for (WXSDKInstance instance : instances) {
instance.fireGlobalEventCallback(eventName, result);
}