第一個問題是,你真的需要移植這些文件嗎? :-)
您可以將Java源代碼包含到Mono for Android項目中;只需將生成操作設置爲AndroidJavaSource
,並將源代碼編譯到生成的.apk文件中。這也可以用.jar
文件完成。
然後出現invoking the Java code from C#的問題。
在IntentIntegration.java
和IntentResult.java
的情況下,可以是不夠,因爲這些類型不支持繼承(他們final
)。當然,使用JNIEnv來調用它們的方法將是一個皮塔餅,但它可以做到:
// Untested code, provided for demo purposes:
// Handle of the Java class we're invoking
IntPtr IntentResult =
JNIEnv.FindClass("com/google/zxing/integration/android/IntentIntegrator");
// Handle of the method to invoke
IntPtr IntentResult_initiateScan =
JNIEnv.GetMethodID(IntentResult, "initiateScan",
"(Landroid/app/Activity;)Landroid/app/AlertDialog;");
// method signature can be obtained from `javap -s`
// Invoke the method; return value is an AlertDialog instance
IntPtr rAlertDialog = JNIEnv.CallStaticObjectMethod (
IntentResult, IntentResult_initiateScan, new JValue (someActivity));
// ...and construct a nice managed wrapper over the Java instance.
AlertDialog alertDialog = new AlertDialog (rAlertDialog);
此外,IntentIntegrator
文檔提到,所提供的活動必須重寫Activity.OnActivityResult方法。
所有這一切說,移植IntentIntegrator.java
不應該是困難的,因爲大部分是包裝過Activity.StartActivityForResult與AlertDialog
(您可能會或可能不會需要)的合適的意圖和建設。
如果你肯定知道這工作讓我知道。我從MonoDroid切換到真正的Android SDK(除了我使用JetBrains的IntelliJ studio),因爲這對我來說必須讓ZXing工作。 – thames 2011-04-25 17:19:20
jonp,我不明白GetMethodID方法的簽名參數。我一直無法在intentintegrator.java上運行javap,因爲它一直在抱怨它找不到該類。請幫忙。你能解釋簽名組件嗎? – 2011-10-23 17:40:48
@CF_Maintainer:最簡單的方法是使用'javap -s -classpath filename.jar package.of.some.Type'。但是,如果你有'.java'文件而不是'.jar'文件,我建議編譯成'.class'文件並使用'javap';否則,您需要手動推斷JNI簽名:http://download.oracle.com/javase/1.5.0/docs/guide/jni/spec/types.html#wp16432和http://www.rgagnon.com /javadetails/java-0286.html – jonp 2011-10-24 14:46:18