7

我正在使用 ZXing 作为库项目的 PhoneGap 条形码扫描仪插件。

我有一个在 Galaxy Tab 2 (7") 上完美运行的代码。相同的代码在 Galaxy S3 上不起作用。

问题:当 ZXing CaptureActivity 扫描条形码时,它只是完成了 CaptureActivity 并且 Calling 活动永远不会通过 onActivityResult 方法返回。

主要节日。

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


}

条码扫描器插件

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 项目作为图书馆项目。

帮助将不胜感激。

4

3 回答 3

5

根据Cordova 网络视图文档

你需要在你的活动中有这个代码:

@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);
}
于 2013-10-09T20:31:59.827 回答
3

尝试cordova.setActivityResultCallback (this);在调用活动之前放置它this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);

于 2016-04-14T18:24:05.953 回答
0

@aviv 给出的答案是绝对正确的,尽管在这种特殊情况下可能不会发生这种情况,但重要的是要添加一个细节:

在插件中为您的意图设置标志时,您必须记住将标志设置为FLAG_ACTIVITY_SINGLE_TOP,而不是FLAG_ACTIVITY_NEW_TASK,否则您的 setResult() 方法将不会按预期返回。

另外,请记住确保您的清单声明 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);
于 2015-12-04T22:47:13.387 回答