Javascript 和 Phonegap 插件之间的异步通信 [英] Asynchronous communication between Javascript and Phonegap Plugin
问题描述
所以,每个人都知道我们创建了一个扩展 CordovaPlugin
的类并覆盖了 execute()
,然后在 JS 和原生 Java(对于 Android)之间创建了一座桥梁.此外,我们使用 PluginResult
将结果返回给 JS.
因此,所有这些都发生在从 JS 向 Java 插件发出请求时.我的问题是,如何将结果发送回 JS(并因此发送回 HTML)异步?
我不知道 异步 就在这里.问题是我想突然将一些东西发送回 JS(例如,当 wifi 启用/禁用时).
我已经对此进行了研究,但没有任何适合我的情况.
我试过的事情是 -
So, everybody knows that we make a Class extending CordovaPlugin
and override the execute()
and then creates a bridge between the JS and native Java (for Android). Further we use PluginResult
to return the result back to the JS.
So, all of this happens when there is a request fired from the JS to the Java Plugin. My question is, how to send a result back to JS (and therefore to HTML) asynchronously ?
I don't know if the word asynchronous is right here. The thing is I want to send something back to the JS out of the blue (say, when wifi becomes enable/disable).
I have already researched on this but haven't got anything which suits to my case.
The thing I've tried is -
- 使用
WifiManager
类创建了一个BroadcastReceiver
来监听WiFi
事件. - 注册接收者.
- 最后,当
WiFi
启用/禁用时弹出一个Toast
,并使用CallbackContext
<代码>callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi已连接")) 和断开与不同的消息.
- Created a
BroadcastReceiver
listening to theWiFi
events using theWifiManager
class. - Registered the receiver.
- And finally, popping a
Toast
whenWiFi
is enabled/disabled, and sending the result usingCallbackContext
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi Connected"))
and for disconnected with a different message.
MyPlugin.java
import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.PluginResult;
import org.json.JSONArray;
...
public class MyPlugin extends CordovaPlugin {
private WifiReceiver wifiBroadcastReceiver = null;
private CallbackContext callbackContext = null;
...
public MyPlugin() {
wifiBroadcastReceiver = new WifiReceiver();
...
}
...
public boolean execute(String action, final JSONArray args,
final CallbackContext callbackId) throws JSONException {
IntentFilter wifiFilter = new IntentFilter(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
cordova.getActivity().registerReceiver(wifiBroadcastReceiver, wifiFilter);
this.callbackContext = callbackId;
...
}
public class WifiReceiver extends BroadcastReceiver{
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
Toast.makeText(cordova.getActivity(), "Wifi Connected", Toast.LENGTH_SHORT).show();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.OK, "Wifi Connected"));
} else {
Toast.makeText(cordova.getActivity(), "Wifi Disconnected", Toast.LENGTH_SHORT).show();
callbackContext.sendPluginResult(new PluginResult(PluginResult.Status.ERROR, "Wifi Disconnected"));
}
}
}
}
Toast
弹出,但 PluginResult
没有发送到 JS.
The Toast
pops but the PluginResult
isn't sent to the JS.
PS:监听 WiFi 事件不是我的实际问题,我想在 Phonegap 中复制 Android 蓝牙聊天
应用程序.因此,它本质上必须是异步的.
PS : Listening to WiFi events isn't my actual problem, I want to replicate the Android Bluetooth Chat
app in Phonegap. So, it has to be asynchronous in nature.
推荐答案
您已经快完成了,但是您需要在您的 PluginResult 上将KeepCallback 设置为 true.如果不这样做,Java 端的后续结果将不会在 JavaScript 端有回调.此类编码的最佳示例是 Cordova 核心中的网络插件.以下是来源链接:
You are almost there but you need to setKeepCallback to true on your PluginResult. If you don't the subsequent results from the Java side will not have a callback on the JavaScript side. The best example of this type of coding is the Network plugin in Cordova core. Here is a link to the source:
因此您应该将代码更新为:
So you should update your code to:
public boolean execute(String action, final JSONArray args,
final CallbackContext callbackId) throws JSONException {
IntentFilter wifiFilter = new IntentFilter(
WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
cordova.getActivity().registerReceiver(wifiBroadcastReceiver,
wifiFilter);
this.callbackContext = callbackId;
PluginResult result = new PluginResult(PluginResult.Status.NO_RESULT);
result.setKeepCallback(true);
this.callbackContext.sendPluginResult(result);
return true;
}
public class WifiReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
PluginResult result;
if (intent.getBooleanExtra(
WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)) {
Toast.makeText(cordova.getActivity(), "Wifi Connected",
Toast.LENGTH_SHORT).show();
result = new PluginResult(PluginResult.Status.OK,
"Wifi Connected");
} else {
Toast.makeText(cordova.getActivity(), "Wifi Disconnected",
Toast.LENGTH_SHORT).show();
result = new PluginResult(PluginResult.Status.ERROR,
"Wifi Disconnected");
}
result.setKeepCallback(false);
if (callbackContext != null) {
callbackContext.sendPluginResult(result);
callbackContext = null;
}
}
}
}
这篇关于Javascript 和 Phonegap 插件之间的异步通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!