android - onActivityResult 永远不会被调用 Phonegap Cordova

标签 android android-intent cordova zxing barcode-scanner

我正在使用 PhoneGap 的 Barcode Scanner Plugin,使用 ZXing 作为库项目。

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

问题:当 ZXing CaptureActivity 扫描条形码时,它刚刚完成 CaptureActivity 并且调用 Activity 永远不会返回 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>

主 Activity .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 projet 作为库项目。

帮助将不胜感激。

最佳答案

根据 Cordova web view documentation

您需要在您的 Activity 中包含此代码:

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

关于android - onActivityResult 永远不会被调用 Phonegap Cordova,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18125415/

相关文章:

android - 铃声模式更改监听器广播接收器?

android - 哪个 Android Intent 用于在 Dropbox 应用程序中获取 'Save to device' 行为?

sdcard的android Intent 准备好了

android - 如何将刚刚创建的对象从一个 Activity 传递到另一个 Activity ?

android - 使用 Phonegap 的 Android 实时视频流?

javascript - 防止 iOS 键盘在 cordova 3.5 中滚动页面

javascript - 本地存储的 json 数组正在打印,但 json 的值在 cordova 应用程序中给出未定义

java - 在android studio上编译时出现错误(1,1)非法字符 '\ufeff'

java - 如何使用 Jsoup 选择没有 ID 或属性的 div?

android - 在不同 Activity 类型之间共享通用功能