第一个问题是,你真的需要移植这些文件吗?:-)
您可以将 Java 源代码包含到 Mono for Android 项目中;只需将 Build 操作设置为AndroidJavaSource
,源代码将被编译到生成的 .apk 中。这也可以通过.jar
文件来完成。
然后是从 C# 调用 Java 代码的问题。
在 and 的情况下IntentIntegration.java
,IntentResult.java
这可能就足够了,因为这些类型不支持继承(它们是final
)。当然,使用JNIEnv在它们上调用方法将是一个 PITA,但可以这样做:
// 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 必须覆盖Activity.OnActivityResult方法。
话虽如此,移植IntentIntegrator.java
应该不是那么困难,因为它大部分是Activity.StartActivityForResult的包装器,具有适当的意图和构造AlertDialog
(您可能需要也可能不需要)。