Example #1
0
        void TestJniInvocation(TextView textview)
        {
            IntPtr Adder = JNIEnv.FindClass("mono/android/test/Adder");

            if (Adder == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder");
            }
            IntPtr Adder_ctor = JNIEnv.GetMethodID(Adder, "<init>", "()V");

            if (Adder_ctor == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder.#ctor()");
            }
            IntPtr Adder_add = JNIEnv.GetMethodID(Adder, "add", "(II)I");

            if (Adder_add == IntPtr.Zero)
            {
                throw new InvalidOperationException("Couldn't find mono.android.test.Adder.add(int,int)");
            }
            IntPtr instance = JNIEnv.NewObject(Adder, Adder_ctor);
            int    result   = JNIEnv.CallIntMethod(instance, Adder_add, new JValue(2), new JValue(3));

            textview.Text += "\n\nnew Adder().add(2,3)=" + result;

            var boundAdder = new Adder(instance, JniHandleOwnership.DoNotTransfer);

            if (boundAdder.Add(3, 4) != 7)
            {
                throw new InvalidOperationException("Add(3,4) != 7!");
            }
            JNIEnv.DeleteLocalRef(instance);
        }
Example #2
0
        void TestManualOverrides()
        {
            var adder = new Adder();

            Console.WriteLine("Adder Class: {0}", adder.Class);

            var managedAdder = new ManagedAdder();
            int result       = Adder.Add(managedAdder, 3, 4);

            if (result != 14)
            {
                throw new InvalidOperationException("ManagedAdder.Add(3, 4) != 14!");
            }

            var progress = new AdderProgress();
            int sum      = Adder.Sum(adder, progress, 1, 2, 3);

            if (sum != 6)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 1, 2, 3) != 6! Was: " + sum + ".");
            }
            if (progress.AddInvocations != 3)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 1, 2, 3) didn't invoke progress 3 times! Was: " + progress.AddInvocations + ".");
            }

            progress.AddInvocations = 0;
            sum = Adder.Sum(managedAdder, progress, 6, 7);
            if (sum != 38)
            {
                throw new InvalidOperationException("Adder.Sum(managedAdder, 6, 7) != 38! Was: " + sum + ".");
            }
            if (progress.AddInvocations != 2)
            {
                throw new InvalidOperationException("Adder.Sum(adder, 6, 7) didn't invoke progress 2 times! Was: " + progress.AddInvocations + ".");
            }

            IntPtr javaDefaultProgress = JNIEnv.CreateInstance("mono/android/test/Adder$DefaultProgress", "()V");
            var    progress2           = Java.Lang.Object.GetObject <IAdderProgress>(javaDefaultProgress, JniHandleOwnership.TransferLocalRef);

            Console.WriteLine("progress2 MCW: {0}", progress2.GetType().FullName);

            javaDefaultProgress = JNIEnv.CreateInstance("mono/android/test/Adder$DefaultProgress", "()V");
            var progress3 = new Java.Lang.Object(javaDefaultProgress, JniHandleOwnership.TransferLocalRef)
                            .JavaCast <IAdderProgress> ();

            Console.WriteLine("progress3 MCW: {0}", progress3.GetType().FullName);
        }
Example #3
0
        static int n_Add(IntPtr jnienv, IntPtr lrefThis, int a, int b)
        {
            Adder __this = Java.Lang.Object.GetObject <Adder>(lrefThis, JniHandleOwnership.DoNotTransfer);

            return(__this.Add(a, b));
        }