简体   繁体   English

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

[英]Worklight 6.0 Barcode or QR code Scanner issue

I am doing POC for QR-code or barcode scanner using worklight 6.0 and using cordova plugin. 我正在使用Worklight 6.0和Cordova插件为QR-codebarcode scanner进行POC I tried to scan the QR code, it's working fine. 我试图扫描QR码,它工作正常。 I was facing issues when I simply start the scanner but I did not anything scan yet. 当我仅启动扫描仪时遇到了问题,但是我还没有进行任何扫描。 Just I press back button on android our apps closed, I need to come back to app from hybrid part. 只要我在android上按后退按钮,我们的应用程序就关闭了,我需要从混合部分返回到应用程序。

Can anyone suggest me. 谁能建议我。 When I cancel the scanner the app is closed, I need come back to hybrids part. 当我取消扫描仪时,应用程序已关闭,我需要回到混合部分。

My Android plugin: 我的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);
    }
}

Callback for JavaScript: 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);
        }

You need to override the back button, to do the same as your RESULT_CANCLE. 您需要覆盖后退按钮,以执行与RESULT_CANCLE相同的操作。 You can see an example of how to override the button, here: Override back button to act like home button as well as in many other seraches for "override android back button". 您可以在此处看到有关如何覆盖按钮的示例: 覆盖后退按钮的作用类似于主页按钮 ,以及在其他许多“覆盖android后退按钮”的练习中。

Something like this code snippet: 类似于以下代码片段:

// 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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM