LibGDX从核心模块(MyGdxGame)中的android模块(AndroidLauncher)获取变量?

时间:2018-07-14 16:19:06

标签: java android libgdx core

所以我看了这篇关于它的文章,它解决了我猜的一些问题。 链接:similar problem here

但是,我使用的是AdMob和Firebase集成,因此我使用了AdView,AdRequest和MobileAds等。我在AndroidLauncher中使用了它们,效果很好,但是在尝试实现名为AdsManager的接口并创建类型变量时AdView它无法解决。我猜是因为我不在android模块中。 当我进入Core模块(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);
}

}

当尝试创建一个实现AdsManager的名为AndroidAdsManager的类时,它无法解析私有的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);
    }
}

1 个答案:

答案 0 :(得分:1)

长话短说:你不能

您好Kevvex,您无法从核心模块中的类访问AdView或AdHandler,要使用它们,您需要做的是(如您提供的链接所示),所有的逻辑都在AndroidAdsManager类中实现,实现您的AdsManager界面

我要解决的方法是,每当您想要对Android模块中的类型进行处理时,在AndroidAdsManager中使用一个方法来执行此操作,您将需要在AdsManagerInterface中创建方法以进行调用:< br /> 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即可调用您所使用的任何方法要从那里来,希望对您有帮助!