- Home /
Unity Android Java Plugin does not work with interface with listener implement
My office has a network library for Android written in Java which enables fast and easy multiplayer networking,
Now, I've been assigned to make the library work on Unity3D, I did my homework on Unity Plugins, AndroidJavaClass, AndroidJavaProxy, AndroidJavaRunnable etc,
So, I can get most Android/Java methods work Like this,
From Java Class -
Class MyClass{
public static Helper helperInstance;
Helper() {
helperInstance = this;
}
public static Helper GetInstance() {
if (helperInstance == null) {
helperInstance = new Helper();
}
return helperInstance;
}
public Context mContext;
public String Test(Context mContext) {
this.mContext = mContext;
Toast.makeText(mContext, "From Java With Love", Toast.LENGTH_SHORT).show();
return "This Works";
}
}
And can access the methods in the class or imported methods from C# like this -
AndroidJavaClass unityDefault = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject gameActivity = unityDefault.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject context = gameActivity.Call<AndroidJavaObject>("getApplicationContext");
AndroidJavaClass hClass = new AndroidJavaClass("com.nixonok.pluginunity.Helper");
AndroidJavaObject helperInstance = RightMeshClass.CallStatic<AndroidJavaObject>("GetInstance");
helloText.text = helperInstance.Call<string>("Test", context);
The problem occurs when I implement the listener for network state change listener:
public interface StateListener {
int SUCCESS = 1;
int FAILURE = 2;
int DISABLED = 3;
int RESUME = 4;
void StateChanged(int var1, int var2);
}
public Class MyClass implements StateListener{
// Called By a Service on network event
@Override
public void meshStateChanged(MeshID meshID, int i) {
}
Helper() {
helperInstance = this;
}
public static Helper GetInstance() {
if (helperInstance == null) {
helperInstance = new Helper();
}
return helperInstance;
}
public Context mContext;
public String Test(Context mContext) {
this.mContext = mContext;
Toast.makeText(mContext, "From Java With Love", Toast.LENGTH_SHORT).show();
return "Now, Even This doesn't work";
}
void init(){
nService nn = new nService(mContext, this);
// Sending context of the listener impliment to the service
}
}
Now, The the plugin won't work at all, Not even the Test method,
implement Interface callback in C# working using AndroidJavaProxy but without the implement, on the Java class, the service won't start :/
Any good idea for me to get this working without needing to change anything in the library?
Thanks in advance :)
Your answer
![](https://koobas.hobune.stream/wayback/20220612155849im_/https://answers.unity.com/themes/thub/images/avi.jpg)