cordova Java交互,Cordova插件:向Java发送事件吗?

So far, so good. However, I'd like to know how to send data/trigger an event in my Javascript code from my plugin - for example, when a user taps an icon in my native code, I'd like my javascript to do something. Is this possible?

解决方案

So I got it to work as follows:

I created a private CallbackContext object in my plugin:

private CallbackContext callbackContext;

and stored the CallbackContext supplied in the execute() method in it:

public boolean execute(final String action, JSONArray args, CallbackContext callbackContext) throws JSONException {

this.callbackContext = callbackContext;

}

Elsewhere in my Java code, I can access this callback and send plugin results to it. This callback will become invalid, however, after it's first triggered, unless keepCallback is set to true:

PluginResult pluginResult = new PluginResult(PluginResult.Status.OK, "WHAT");

pluginResult.setKeepCallback(true);

callbackContext.sendPluginResult(pluginResult);

This made me a happy camper. I hope it helps someone else!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值