我有一个Cordova插件,它使用名为" com.hyipc.core.service.barcode.BarcodeService2D"的服务扫描条形码。这是通过向广播接收器注册接收器来完成的。
扫描过程成功运行,但我想将扫描结果发送回我的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);
}
}
}
}
答案 0 :(得分:3)
您需要将回调上下文参数传递给BarcodeReceiver类:
Assert