- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个使用名为“com.hyipc.core.service.barcode.BarcodeService2D”的服务扫描条形码的 Cordova 插件。这是通过向广播接收器注册接收器来完成的。
扫描过程运行成功,但我想将扫描结果发送回我的 cordova 应用程序,我看到这是使用 callbackContext.success(strBarcode)
完成的。问题是我不能在我的 BarcodeReceiverClass 中使用它。有什么方法可以将数据从 onReceive
内部发送回我的应用程序?
public class Scanner extends CordovaPlugin {
private BroadcastReceiver mReceiver = new BarcodeReceiver();
private Intent intentService = new Intent("com.hyipc.core.service.barcode.BarcodeService2D");
public static final String ACTION_BARCODE_SERVICE_BROADCAST = "action_barcode_broadcast";
public static final String KEY_BARCODE_STR = "key_barcode_string";
private String strBarcode = "";
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
if (action.equals("scan")) {
scan();
return true;
} else {
return false;
}
}
public void scan() {
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_BARCODE_SERVICE_BROADCAST);
cordova.getActivity().startService(intentService);
cordova.getActivity().registerReceiver(mReceiver, filter);
}
public class BarcodeReceiver extends BroadcastReceiver {
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().equals(ACTION_BARCODE_SERVICE_BROADCAST)) {
strBarcode = intent.getExtras().getString(KEY_BARCODE_STR);
callbackContext.success(strBarcode);
}
}
}
}
最佳答案
您需要将回调上下文参数传递给您的 BarcodeReceiver 类:
public class Scanner extends CordovaPlugin {
....
private BroadcastReceiver mReceiver = null;
@Override
public boolean execute(String action, JSONArray data, CallbackContext callbackContext) throws JSONException {
....
mReceiver = new BarcodeReceiver(callbackContext);
....
}
....
}
public class BarcodeReceiver extends BroadcastReceiver {
private CallbackContext callbackContext;
public BarcodeReceiver (CallbackContext callbackContext) {
this.callbackContext = callbackContext;
}
public void onReceive(Context ctx, Intent intent) {
if (intent.getAction().equals(ACTION_BARCODE_SERVICE_BROADCAST)) {
strBarcode = intent.getExtras().getString(KEY_BARCODE_STR);
callbackContext.success(strBarcode);
}
}
}
关于java - 在 BroadcastReceiver 中使用 callbackContext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38391275/
我正在使用新的输入系统并调用 Unity 事件将输入传递给我的 bevaviour 脚本。这是显示通过输入进行简单移动的示例代码 public class MovementBehaviour : Mo
我有一个使用名为“com.hyipc.core.service.barcode.BarcodeService2D”的服务扫描条形码的 Cordova 插件。这是通过向广播接收器注册接收器来完成的。 扫
我在 Metro 模式下实现线程,我在谷歌搜索时得到了这个例子,但我不明白 CallbackContext 。这有什么用? This解释了它在 MTA 中的用途,但我不清楚。当我将 CallbackC
我正在尝试更新在 github 上找到的一个尚未更新的插件 (AdMob) https://github.com/rajpara11/phonegap-plugins/blob/master/Andr
我是一名优秀的程序员,十分优秀!