Worklight 6.0条形码或QR码扫描仪问题

时间:2015-06-12 11:25:07

标签: android ibm-mobilefirst cordova-plugins

我使用worklight 6.0并使用cordova插件为POCQR-codebarcode scanner。我试图扫描二维码,它工作正常。当我只是启动扫描仪时我遇到了问题,但我还没有扫描任何东西。只是我按下我们的应用程序关闭的Android按钮,我需要从混合部分回到应用程序。

任何人都可以建议我。当我取消扫描仪应用程序关闭时,我需要回到混合动力部分。

我的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);
        }

1 个答案:

答案 0 :(得分:0)

您需要覆盖后退按钮,以与RESULT_CANCLE相同。你可以在这里看到一个如何覆盖按钮的例子:Override back button to act like home button以及“覆盖android后退按钮”的许多其他seraches。

像这段代码片段:

// 2.0 and above
@Override
public void onBackPressed() {
  // code here
}

// Before 2.0
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
      // code here
    }
    return super.onKeyDown(keyCode, event);
}