第一个问题是,你真的需要移植这些文件吗? :-)
您可以将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