- Home /
Can't call non-static method in AndroidJavaClass
I have do the searches for a day and can't find the solution. I want to call a method in an android plugin. I have compiled to jar file and import to Plugins\Android\bin folder, and successfully call a static method to send sms. But I want to get phone's number, so I have to write another method in android java class. Since it's not a static method, I have learned that I have to create an instance of the class to call it. But I can't do that. I have tried several solutions. Here is my code:
public class SMSActivity extends UnityPlayerActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public String getPhoneNumber() {
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
return telemamanger.getLine1Number();
}
}
in Unity I call the class with:
ajc = new AndroidJavaClass("com.illidan.sms.SMSActivity");
and create an instance of the class:
ajo = ajc.Get<AndroidJavaObject>("currentActivity");
But it's not working. The code stops at that line, in logcat I get no static field with name currentActivity. It's really weird because I found some question about this, and according to the replies, that code was working.
Answer by Karthy07 · Feb 22, 2016 at 07:56 PM
@kactus223 "currentActivity" is in the class "com.unity3d.player.UnityPlayer"
So the correct call would be ...
ajc = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
ajo = ajc.Get("currentActivity");