Android Worklight 6.0条形码或二维码扫描仪问题
我正在使用worklight 6.0和cordova插件为Android Worklight 6.0条形码或二维码扫描仪问题,android,ibm-mobilefirst,cordova-plugins,Android,Ibm Mobilefirst,Cordova Plugins,我正在使用worklight 6.0和cordova插件为二维码或条形码扫描仪进行POC。我试着扫描二维码,效果很好。当我简单地启动扫描仪时,我遇到了一些问题,但我还没有扫描任何东西。只要我按下android上的“后退”按钮,我们的应用程序就关闭了,我需要从混合部分返回应用程序 有人能推荐我吗。当我取消扫描器的应用程序是关闭的,我需要回来的一部分 我的Android插件: import org.apache.cordova.api.CallbackContext; import org.apac
二维码
或条形码扫描仪
进行POC
。我试着扫描二维码,效果很好。当我简单地启动扫描仪时,我遇到了一些问题,但我还没有扫描任何东西。只要我按下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);
}
您需要覆盖“后退”按钮,以执行与结果相同的操作。您可以在这里看到一个如何覆盖按钮的示例:以及许多其他seraches中的“覆盖android后退按钮” 类似于以下代码段:
// 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);
}
我的问题是当点击后退按钮或关闭相机时,应用程序也会关闭。我需要回到hybridYes,我知道如何阅读。您需要或重写默认行为。