- Home /
How to call a Android native class with implements from unity ?
Hi ,
How to call a native android method from Unity ?
native method :
public void sharePhoto(final String textPath, final String imagePath) {
}
From Unity am calling like :
testClass.Call("sharePhoto","hai Sir","http://stereo.gsfc.nasa.gov/beacon/latest_256/ahead_euvi_195_latest.jpg");
Result :
am not getting any error. but unable to enter into the method.
How to call ?
Answer by gfoot · Feb 04, 2014 at 09:03 PM
I think the problem is that testClass is an AndroidJavaClass - that is, it refers to the class itself, not to an instance of the class. That makes it good for calling static methods, but not non-static ones.
You need to create an instance instead, using AndroidJavaObject and issue the Call method on that, using the same syntax you're using now.
AndroidJavaObject testObject = new AndroidJavaObject(className, constructorArg1, constructorArg2, ...);
testObject.Call(methodName, methodArg1, methodArg2, ...);
The first line constructs the object by calling an appropriate constructor, dependent on the arguments you pass. The second line calls a method on the object.
yes @gfoot,
I have tried the same as your comment
Unity Code :
AndroidJavaClass _$$anonymous$$yActivityClass = new AndroidJavaClass("com.unity3d.player.TestProjectActivity");
AndroidJavaObject _ActivityObject = _$$anonymous$$yActivityClass.GetStatic<AndroidJavaObject>("currentActivity");
_ActivityObject.Call("sharePhoto","hai Sir","http://stereo.gsfc.nasa.gov/beacon/latest_256/ahead_euvi_195_latest.jpg");
am getting an error like : "NoSuchFieldError : no static field with name='CurrentActivity' .......
( refer attached Image)][1]
How to create an instance ?
See this question regarding accessing currentActivity.
The main point is that you have to request the currentActivity from com.unity3d.player.UnityPlayer, not from your own class. The object you get back is still going to be of your derived type (assu$$anonymous$$g you set the manifest up correctly) and so you can call your sharePhoto method directly on that.
yes gfoot ... i also tried with com.unity3d.player.UnityPlayer , geting an error "NoSuchFieldError : no static field with name='CurrentActivity' .......
@toritobravom, this question diverged from the original topic - it started being about calling methods, then turned into getting the current activity reference. It may be better to ask a separate one.