0
我在做POC
为QR-code
或barcode scanner
使用worklight 6.0和使用cordova插件。我试图扫描QR码,它工作正常。当我简单地启动扫描仪时,我遇到了问题,但我还没有进行任何扫描。只要我按下了我们的应用程序关闭的Android按钮,我需要从混合部分回来的应用程序。Worklight 6.0条码或QR码扫描仪问题
任何人都可以建议我。当我取消扫描仪的应用程序关闭时,我需要回到混合动力部分。
我的Android插件:
import org.apache.cordova.api.CallbackContext;
import org.apache.cordova.api.CordovaPlugin;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
public class BarcodeScanner extends CordovaPlugin {
public static final int REQUEST_CODE = 0x0ba7c0de;
private static final String SCAN = "scan";
private static final String ENCODE = "encode";
private static final String CANCELLED = "cancelled";
private static final String FORMAT = "format";
private static final String TEXT = "text";
private static final String DATA = "data";
private static final String TYPE = "type";
private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN";
private static final String ENCODE_DATA = "ENCODE_DATA";
private static final String ENCODE_TYPE = "ENCODE_TYPE";
private static final String ENCODE_INTENT = "com.phonegap.plugins.barcodescanner.ENCODE";
private static final String TEXT_TYPE = "TEXT_TYPE";
private static final String EMAIL_TYPE = "EMAIL_TYPE";
private static final String PHONE_TYPE = "PHONE_TYPE";
private static final String SMS_TYPE = "SMS_TYPE";
private static final String LOG_TAG = "BarcodeScanner";
private CallbackContext callbackContext;
/**
* Constructor.
*/
public BarcodeScanner() {
}
@Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) {
this.callbackContext = callbackContext;
Log.d(LOG_TAG, "*************************** EXECUTE ************************************");
if (action.equals(ENCODE)) {
JSONObject obj = args.optJSONObject(0);
if (obj != null) {
String type = obj.optString(TYPE);
String data = obj.optString(DATA);
// If the type is null then force the type to text
if (type == null) {
type = TEXT_TYPE;
}
if (data == null) {
callbackContext.error("User did not specify data to encode");
return true;
}
encode(type, data);
} else {
callbackContext.error("User did not specify data to encode");
return true;
}
} else if (action.equals(SCAN)) {
Log.d(LOG_TAG, "*************************** SCAN START ************************************");
scan();
} else {
Log.d(LOG_TAG, "*************************** SCAN FALSE ************************************");
return false;
}
return true;
}
/**
* Starts an intent to scan and decode a barcode.
*/
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Log.d(LOG_TAG, "*************************** RESULT OK ************************************");
JSONObject obj = new JSONObject();
try {
obj.put(TEXT, intent.getStringExtra("SCAN_RESULT"));
obj.put(FORMAT, intent.getStringExtra("SCAN_RESULT_FORMAT"));
obj.put(CANCELLED, false);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
this.callbackContext.success(obj);
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.d(LOG_TAG, "*************************** RESULT CANCELED ************************************");
JSONObject obj = new JSONObject();
try {
obj.put(TEXT, "cancel");
obj.put(FORMAT, "cancel");
obj.put(CANCELLED, true);
} catch (JSONException e) {
Log.d(LOG_TAG, "This should never happen");
}
//this.success(new PluginResult(PluginResult.Status.OK, obj), this.callback);
this.callbackContext.success(obj);
//this.cordova.getActivity().finish();
} else {
//this.error(new PluginResult(PluginResult.Status.ERROR), this.callback);
this.callbackContext.error("Unexpected error");
}
}
}
/**
* Initiates a barcode encode.
*
* @param type Endoiding type.
* @param data The data to encode in the bar code.
*/
public void encode(String type, String data) {
Intent intentEncode = new Intent(ENCODE_INTENT);
intentEncode.putExtra(ENCODE_TYPE, type);
intentEncode.putExtra(ENCODE_DATA, data);
this.cordova.getActivity().startActivity(intentEncode);
}
}
回调的JavaScript:
function onScan(){
cordova.exec(onScanSuccess, onScanFailure, 'BarcodeScanner', 'scan', []);
}
function onScanSuccess(result) {
console.log("onScanSuccess @@@@@@@@@@@@@@@@@@@@ text ==" + result.text);
console.log("onScanSuccess @@@@@@@@@@@@@@@@@@@@ format == " + result.format);
}
function onScanFailure(error) {
console.log("onScanFailure @@@@@@@@@@@@@@@@@@@@ " + result);
}
我的问题是点击后退按钮或关闭摄像头的应用也接近我需要回过头来混合 – karthik
是的,我知道如何阅读的时候。您需要或*覆盖*默认行为。 –