LibGDX从Core模块(MyGdxGame)中的android模块(AndroidLauncher)获取变量?
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LibGDX从Core模块(MyGdxGame)中的android模块(AndroidLauncher)获取变量?相关的知识,希望对你有一定的参考价值。
所以我看过这篇关于它的文章,它解决了我猜的一些问题。链接:similar problem here
但是,我正在使用AdMob和Firebase集成,因此我使用AdView,AdRequest和MobileAds等。我在androidLauncher中使用这些并且工作正常,但在尝试实现名为AdsManager的界面并创建AdView类型的变量时,它可以解决它。我想这是因为我不在android模块中。那么当我进入核心模块(MyGdxGame)时,如何从AndroidLauncher获取“AdMob类型”变量?
示例:Android模块
的AdsManager
package com.game.mygame.monetization;
public interface AdsManager {
void showAds(boolean show);
}
AndroidLauncher
public class AndroidLauncher extends AndroidApplication implements AdsManager {
private final String TAG = "AndroidLauncher";
private AdHandler adHandler; // Variable I want to reach from core module
static class AdHandler extends Handler {
private final int ADS_SHOW = 1;
private final int ADS_HIDE = 0;
private AdView adBanner; // Variable I want to reach from core module
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what) {
case ADS_SHOW:
adBanner.setVisibility(View.VISIBLE);
break;
case ADS_HIDE:
adBanner.setVisibility(View.GONE);
break;
}
}
}
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration config = new AndroidApplicationConfiguration();
RelativeLayout layout = new RelativeLayout(this);
View view = initializeForView(new MyGdxGame(this), config);
layout.addView(view);
implementAds();
requestAds(layout);
// Sets the background image
setContentView(layout);
}
public void implementAds() {
// Implement ads
MobileAds.initialize(this, "");
adHandler = new AdHandler();
adHandler.adBanner = new AdView(this);
adHandler.adBanner.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.i(TAG, "Ad loaded ...");
}
});
adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
adHandler.adBanner.setAdUnitId("");
}
public void requestAds(RelativeLayout layout) {
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
builder.addTestDevice("");
RelativeLayout.LayoutParams adBannerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.addView(adHandler.adBanner, adBannerParams);
adHandler.adBanner.loadAd(builder.build());
}
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
}
当试图创建一个名为AndroidAdsManager的类来实现AdsManager时,它无法解析私有的AdView变量,因为我不再在Android模块中。
核心模块
public class AndroidAdsManager implements AdsManager {
private AdView adBanner; // Error: Android Studio doesn't find this type
private AdHandler adHandler; // Also needs this in here, I guess, maybe not
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
}
长话短说:你不能 您好Kevvex,您无法从核心模块中的类访问AdView或AdHandler,您需要做的就是(如您提供的链接所示)在AndroidAdsManager类中实现所有逻辑实现您的AdsManager界面 关于这个问题的方法是,无论何时你想要使用Android模块中的类型进行某些操作,在AndroidAdsManager中都有一个方法,你需要在AdsManagerInterface中创建方法来调用它们: AndroidAdsManager
public class AndroidAdsManager implements AdsManager {
private AdView adBanner;
private AdHandler adHandler;
@Override
public void showAds(boolean show) {
adHandler.sendEmptyMessage(show ? adHandler.ADS_SHOW : adHandler.ADS_HIDE);
}
@Override
public void implementAds() {
// Implement ads
MobileAds.initialize(this, ""); // HERE YOU NEED TO GET THE CONTEXT
adHandler = new AdHandler();
adHandler.adBanner = new AdView(this);
adHandler.adBanner.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.i(TAG, "Ad loaded ...");
}
});
adHandler.adBanner.setAdSize(AdSize.SMART_BANNER);
adHandler.adBanner.setAdUnitId("");
}
// You need to get a RelativeLayout reference for this method to work
// I would get it from AndroidLauncher
public void requestAds() {
AdRequest.Builder builder = new AdRequest.Builder();
builder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
builder.addTestDevice("");
RelativeLayout.LayoutParams adBannerParams = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
adBannerParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
layout.addView(adHandler.adBanner, adBannerParams);
adHandler.adBanner.loadAd(builder.build());
}
}
的AdsManager
public interface AdsManager {
void implementAds();
void requestAds(); // Relative layout param removed from here
void showAds(boolean show);
}
在此之后,如果您希望这些操作影响您的游戏,只需将您的游戏实例(核心模块)的引用传递给AndroidAdsManager
中的AndroidLauncher
,以便能够从那里调用您想要的任何方法,希望这会有所帮助!
以上是关于LibGDX从Core模块(MyGdxGame)中的android模块(AndroidLauncher)获取变量?的主要内容,如果未能解决你的问题,请参考以下文章