Accessing the Activity Context in Android plugin

Is there a way to access the Activity or Application Context for plugin development?

Say I have a method like this:

public static void myMethod(Context context, String s1, String s2)

How would I call this method from Unity?

Right now I'm trying something like this. The problem is obj_Activity is an Activity type ? How would I pass in the App or Activity Context in this case?

        // first we try to find our main activity..
    IntPtr cls_Activity = JNI.FindClass("com/unity3d/player/UnityPlayer");
    int fid_Activity = JNI.GetStaticFieldID(cls_Activity, "currentActivity", "Landroid/app/Activity;");
    obj_Activity = JNI.GetStaticObjectField(cls_Activity, fid_Activity);
    Debug.Log("obj_Activity = " + obj_Activity);

    AndroidJavaClass myClass = new AndroidJavaClass("com.test.myClass"); 

    myClass.CallStatic("myMethod", 
                            obj_Activity, 
                            new AndroidJavaObject("java.lang.String", "string1"), 
                            new AndroidJavaObject("java.lang.String", "string2"));

1 Answer

1

Hm I think I figured it out:

AndroidJavaClass jc = new AndroidJavaClass("com.unity3d.player.UnityPlayer"); 
AndroidJavaObject jo = jc.GetStatic<AndroidJavaObject>("currentActivity");

hi shara i am newer unity3d developer i need some help from you i followed below documentation but i am not getting properly any help from your side.please help me.i am getting this lines Unable to find method id for 'Launch' UnityEngine.AndroidJavaObject:Call(String, Object[]) AndroidGui:OnGUI() (at Assets/AndroidGui.cs:16) file:///Applications/Unity/Unity.app/Contents/Documentation/Documentation/Manual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html

THanks its help me

It helps to add a 'setContext' method to your plugin. You can then pass the context data to the plugin after you acquire it using the code you wrote. I painstakingly managed to [write and use a plugin][1] to detect hardware buttons on an Android device. At first I was a bit baffled as to how to retrieve the actual context in use but your snippet helped to demystify that one. [1]: http://answers.unity3d.com/questions/1163907/how-can-i-detect-if-the-back-button-is-virtual-or.html#answer-1165286