Calling an Android method from Unity

Hello All,

I followed the link here http://unity3d.com/support/documentation/Manual/Android-Integrating%20Unity%20With%20Eclipse.html to integrate eclipse and unity.

I am successfully able to run an unity from eclipse.

What I really want to do is I have a button in Unity interface. And when that button is pressed, I want to call a function foo() in Android Activity.

But the problem is I cannot call this function inside android Activity . My code is as follows

Android:

public class MainActivity extends UnityPlayerActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }
    
   // call this method from unity
    public static void foo(){
    	Log.d("TEST","Foo method is called is called");  // this method is never called
    	
    }
}

And in Unity cSharp script in button press handler my code is

if(GUI.Button(new Rect(20,70,80,20), "Call Android Method")) {

		using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {

			using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {

				AndroidJavaClass cls_MainActivity = new AndroidJavaClass("com.me.callActivity.MainActivity");

				cls_MainActivity.CallStatic("Init", obj_Activity);

				cls_MainActivity.CallStatic("foo");
				

			}
}

My applilcation doesn’t crash. But when i see the log in eclipse i get the JNI error

JNI: Unable to find method id for ‘foo’ (static)

Am I missing something? Do I have to do manually as described in

Thank you,
BlackDiamond

I think you’ve got one step too many in there. The current activity which you’re getting is the main activity so you should be using that to call your methods.

Try:

if(GUI.Button(new Rect(20,70,80,20), "Call Android Method")) 
{
    using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) 
    {
        using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) 
        {
            obj_Activity .CallStatic("foo");
        }
    }
}

Hello HazeTI,

I did as you said and it works now.

Thank you,
BlackDiamond

My issue is that I need to call startActivity in foo function (switch from unity view to another activity android). I can get context by passing parameter obj_Activity from unity to foo funtion

using (AndroidJavaClass cls_UnityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) {
			using (AndroidJavaObject obj_Activity = cls_UnityPlayer.GetStatic<AndroidJavaObject>("currentActivity")) {
				AndroidJavaClass cls_MainActivity = new AndroidJavaClass("com.example.TestActivity");
				cls_MainActivity.CallStatic("foo", obj_Activity);
			}
		}

In foo function, my code is

public static void foo(Context c){
        Intent intent = c.getPackageManager().getLaunchIntentForPackage("com.example.NextActivity");		
		c.startActivity(intent);
	}

It cause errors

AndroidJavaException: java.lang.NullPointerException
at UnityEngine.AndroidJNISafe.CheckException () [0x00000] in <filename unknown>:0 
...

It seems like it cannot find package name. Can anyone help me?

Anybody can help? I really need this!

Anyway, I have found solution. It is no need to implement java file and add it in Plugins/Android like this tutorial Accessing the Android Compass through Unity 3D. | Random Acts of Dev...

You just make an activity A extends UnityPlayerActivity and add meta code in that activity in AndroidManifest.xml

<meta-data
                android:name="unityplayer.ForwardNativeEventsToDalvik"
                android:value="false" />

If you want to call method in android from unity, simply declare these static method in activity A

Here is my simple Unity project and Android project. My Android project has 3 activities switch between Android and Unity: WelcomeActivity (Android) ↔ MainActivity (Unity) ↔ PhotoActivity (Android). My Unity project has a cube moving around with two next and back button to switch screen.
http://www.mediafire.com/download/ejfjsega9cdqxdm/UnityAndroidIntegration.zip

Some useful tutorial here
http://www.rbcafe.com/Softwares/Unity/Documentation/Manual/Android-Launch%20an%20Android%20Application%20from%20a%20Unity%20Application.html

I’m sorry for the wrong source code uploaded. This is a new version has been tested
http://www.mediafire.com/download/wdbcmx3iczl924a/UnityAndroidIntegration_updated.zip

file missing