onActivityResult永远不会被称为Phonegap Cordova

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

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

问题:当ZXing CaptureActivity扫描条形码时,只需完成CaptureActivity和Calling活动就不会返回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作为图书馆项目。

帮助将不胜感激。

Related of "onActivityResult永远不会被称为Phonegap Cordova"

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

尝试把这个cordova.setActivityResultCallback (this); 就在调用活动this.cordova.startActivityForResult((CordovaPlugin) this, intentScan, AppConstants.CAMERA_SCAN_REQUEST_CODE);

在我的cordova插件类中,在我调用startActivityForResult方法之前,我将此类设置为结果callback。

 cordova.setActivityResultCallback(this); 

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