Reputation: 173
I need to read the value of some variables that are in AndroidLauncher on Android package through a class in Core package. As below, I need to read the value of interstitial variable.
AndroidLaucher on Android:
public static int interstital=0;
public int ads() {
// TODO Auto-generated method stub
return interstital;
}
GameScreen on Core:
System.out.println(AndroidLaucher.ads());
I tried to implement an interface, but it does not support static methods. How to return the value of this variable on AndroidLauncher to GameScreen on Core?
Thanks
Upvotes: 1
Views: 641
Reputation: 918
Don't use static methods, especially on android, create an Ads interface like this:
public interface AdsManager {
public void showAds();
public Integer getInterstital();
}
Make an android implementation of it:
public class AndroidAdsManager implements AdsManager{
private Integer interstital;
public AndroidAdsManager(){
interstital = 0;
}
@Override
public void showAds(){
//Show ads method
}
@Override
public Integer getInterstital(){
return interstital;
}
}
now on your android launcher you create the instance and pass the java reference to your game:
public class AndroidLauncher extends AndroidApplication {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
AdsManager androidManager = new AndroidAdsManager();
initialize(new MyGame(androidManager), config);
}
}
And finally on your game class:
public class MyGame extends ApplicationAdapter {
private AdsManager adsManager;
public Mygame(AdsManager adsManager){
this.adsManager = adsManager;
}
}
Now you can use your adsManager anywhere on your code, it will use the methods from the implementations if it was instanced on desktop, android or ios.
Also, as mentionned in the comments: "When using LibGDX and you want to get information or control components from specific platforms, use interfaces. Just be mindful that you may need to design it so that it works on all platforms even if they don't use ads."
Upvotes: 2