Sweet thanks (though the string value actually has to be a Java.Lang.String or 
it won't compile)

For those interested, these are the two calls you need to track sessions in 
flurry (for testing, they have been dumped into where I am supposed to "call" 
them from, which is onStart and onStop of an activity you want to track.)

protected override void OnStart()
        {
            base.OnStart();
            try
            {
                IntPtr Flurry_Class = 
JNIEnv.FindClass("com/flurry/android/FlurryAgent");
                IntPtr Flurry_onStartSession = JNIEnv.GetStaticMethodID 
(Flurry_Class, "onStartSession", 
"(Landroid/content/Context;Ljava/lang/String;)V");
                Java.Lang.String key = new Java.Lang.String("YOUR KEY HERE");
                
JNIEnv.CallStaticVoidMethod(Flurry_Class,Flurry_onStartSession,new 
JValue(this),new JValue(key));

            }
            catch
            {
            }
        }
        protected override void OnStop()
        {
            base.OnStop();
            try
            {
                IntPtr Flurry_Class = 
JNIEnv.FindClass("com/flurry/android/FlurryAgent");
                IntPtr Flurry_onEndSession = 
JNIEnv.GetStaticMethodID(Flurry_Class, "onEndSession", 
"(Landroid/content/Context;)V");
                JNIEnv.CallStaticVoidMethod(Flurry_Class, Flurry_onEndSession, 
new JValue(this));

            }
            catch
            {
            }
        }


Obviously it needs to be wrapped up cleaner into a utility library.. But these 
to actually work and send data to Flurry with Mono for Android 4.0 just include 
the Flurry  jar file.  But looking at the Android logs, Flurry did get called, 
and send data to their analytics service.. So as a POC it does work :-)..

Josh


-----Original Message-----
From: monodroid-boun...@lists.ximian.com 
[mailto:monodroid-boun...@lists.ximian.com] On Behalf Of Jonathan Pryor
Sent: Wednesday, December 07, 2011 10:32 AM
To: Discussions related to Mono for Android
Subject: Re: [mono-android] Calling Static JNI methods?

On Dec 7, 2011, at 11:16 AM, Josh Handel wrote:
> I am working on trying to wrap the Flurry API... Flurry uses static methods 
> for all of its calls.. I haven't seen/figured out how to call static methods 
> thru JNI

JNI differentiates between instance and static members, so to lookup a static 
method you need JNIEnv.GetStaticMethodID():

        
http://androidapi.xamarin.com/?link=M:Android.Runtime.JNIEnv.GetStaticMethodID

Similarly, to invoke the method you'll need e.g. 
JNIEnv.CallStaticObjectMethod():

        
http://androidapi.xamarin.com/?link=M:Android.Runtime.JNIEnv.CallStaticObjectMethod

> or (better) use the new Register stuff to wrap the Flurry class (specificly 
> its static methods).

[Register] isn't used to implement the method invocation. [Register] is used to 
permit subclassing, interface implementation, and overriding/implementing 
class/interface methods. (It's used to control Android Callable Wrapper 
generation, i.e. Java generation, and thus doesn't do anything for the C# code 
you need to write.)

> I can locate the class via JNI or Java.Lang.Class.FromName()  but given these 
> are static methods not instance methods I'm not sure where to go next..
>  
> FlurryAgent.onStartSession(Context,"key");

As always, use `javap -s ...` to determine the correct JNI signature. I'll just 
guess for demonstration purposes, though:

        IntPtr FlurryAgent_Class = JNIEnv.FindClass ("package/of/FlurryAgent");
        IntPtr FlurryAgent_onStartSession = JNIEnv.GetStaticMethodID 
(FlurryAgent_Class,
                        "onStartSession", 
"(Landroid/content/Context;Ljava/lang/String;)V");
        JNIEnv.CallStaticVoidMethod (FlurryAgent_Class, 
FlurryAgent_onStartSession,
                        new JValue (Context), new JValue ("key"));


> FlurryAgent.onStopSession(Context);

        IntPtr FlurryAgent_onStopSession = JNIEnv.GetStaticMethodID 
(FlurryAgent_Class,
                        "onStartSession", "(Landroid/content/Context;)V");
        JNIEnv.CallStaticVoidMethod (FlurryAgent_Class, 
FlurryAgent_onStopSession,
                        new JValue (Context));

 - Jon

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

_______________________________________________
Monodroid mailing list
Monodroid@lists.ximian.com

UNSUBSCRIBE INFORMATION:
http://lists.ximian.com/mailman/listinfo/monodroid

Reply via email to