我使用ZXing作为图书馆项目为PhoneGap使用Barcode Scanner插件。
我有一个在Galaxy Tab 2(7“)上完美运行的代码。相同的代码在Galaxy S3上不起作用。
问题:当ZXing CaptureActivity扫描条形码时,它只是完成了CaptureActivity,并且调用活动永远不会带有onActivityResult方法。
MainFest。
<activity
android:name=".activity.MainActivity"
android:configChanges="orientation|keyboardHidden|keyboard|screenSize|locale"
android:label="@string/app_name"
android:theme="@android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.google.zxing.client.android.CaptureActivity"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
android:windowSoftInputMode="stateAlwaysHidden" >
<intent-filter>
<action android:name="com.phonegap.plugins.barcodescanner.SCAN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
MainActivity.java
public void startActivityForResult(CordovaPlugin command, Intent intent,
int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return
// results
if (command != null) {
this.keepRunning = false;
}
// Start activity
startActivityForResult(intent, requestCode);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
CordovaPlugin callback = this.activityResultCallback;
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, intent);
} else {
Log.e(TAG, "Plugin callback null");
}
// else continue with any other code you need in the method
super.onActivityResult(requestCode, resultCode, intent);
}
BarcodeScanner插件
private static final String SCAN_INTENT = "com.phonegap.plugins.barcodescanner.SCAN";
public void scan() {
Intent intentScan = new Intent(SCAN_INTENT);
intentScan.addCategory(Intent.CATEGORY_DEFAULT);
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan,
AppConstants.CAMERA_SCAN_REQUEST_CODE);
}
我将ZXing projet作为图书馆项目。
帮助将不胜感激。
答案 0 :(得分:5)
每Cordova web view documentation
您需要在活动中使用此代码:
@Override
public void setActivityResultCallback(CordovaPlugin plugin) {
this.activityResultCallback = plugin;
}
public void startActivityForResult(CordovaPlugin command, Intent intent, int requestCode) {
this.activityResultCallback = command;
this.activityResultKeepRunning = this.keepRunning;
// If multitasking turned on, then disable it for activities that return results
if (command != null) {
this.keepRunning = false;
}
// Start activity
super.startActivityForResult(intent, requestCode);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
CordovaPlugin callback = this.activityResultCallback;
if (callback != null) {
callback.onActivityResult(requestCode, resultCode, intent);
}
}
以及:
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);
您需要在插件中使用以下方法:
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
//do something with the result
super.onActivityResult(requestCode, resultCode, intent);
}
答案 1 :(得分:3)
在调用活动cordova.setActivityResultCallback (this);
this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);
答案 2 :(得分:1)
在我的cordova插件类中,在调用startActivityForResult方法之前,我将此类设置为结果回调。
cordova.setActivityResultCallback(this);
答案 3 :(得分:0)
@aviv给出的答案是绝对正确的,虽然在这种特殊情况下可能没有发生,但重要的是要添加一个细节:
在插件中设置意图的标记时,必须记住将标记设置为FLAG_ACTIVITY_SINGLE_TOP
,而不是FLAG_ACTIVITY_NEW_TASK
,否则您的setResult()方法将不会按预期返回。< / p>
另外,请记住确保您的清单声明了launchMode:
android:launchMode="singleTop"
在Android中有一个奇怪的问题,如果你单独运行似乎工作正常,但如果你作为单个实例或单个任务运行,结果将立即返回。
因此,请记住在您要从插件中启动本机图层时添加此内容
Intent i = new Intent(cordova.getActivity(), ThanksActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
cordova.startActivityForResult(this, i, 0);
PluginResult r = new PluginResult(PluginResult.Status.NO_RESULT);
r.setKeepCallback(true);
callbackContext.sendPluginResult(r);