- Home /
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"));
Answer by shara · Apr 20, 2011 at 09:11 PM
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/$$anonymous$$anual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html
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 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.
Your answer
Follow this Question
Related Questions
Android Manifest Help 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Android plugins and callbacks on non-scripting threads 1 Answer
Soomla Error for android 1 Answer
Application.OpenURL not working 1 Answer