第一个问题是,您真的需要移植这些文件吗? :-)
您可以将 Java 源代码包含到 Mono for Android 项目中;只需将构建操作设置为AndroidJavaSource
并且源代码将被编译到生成的 .apk 中。这也可以通过以下方式完成.jar
files.
那么问题来了从 C# 调用 Java 代码 http://mono-android.net/Documentation/API_Design#Jave_Native_Interface_Support.
如果是IntentIntegration.java
and IntentResult.java
, that may就足够了,因为这些类型不支持继承(它们是final
)。授予,使用JNIEnv http://docs.mono-android.net/index.aspx?link=T%3aAndroid.Runtime.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 http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.OnActivityResult(System.Int32%2cAndroid.App.Result%2cAndroid.Content.Intent) method.
综上所述,移植IntentIntegrator.java
不应该that困难,因为大部分都是包装Activity.StartActivityForResult http://docs.mono-android.net/index.aspx?link=M%3aAndroid.App.Activity.StartActivityForResult(Android.Content.Intent%2cSystem.Int32)具有适当的意图和构建AlertDialog
(您可能需要也可能不需要)。