gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 23:46:23 24 4
gpt4 key购买 nike

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

但是,我使用的是 AdMob 和 Firebase 集成,因此我使用 AdView、AdRequest 和 MobileAds 等。我在 AndroidLauncher 中使用它们并且工作正常,但是当尝试实现一个名为 AdsManager 的接口(interface)并创建一个类型的变量时AdView 无法解析。我猜是因为我脱离了android模块。 那么,当我在核心模块 (MyGdxGame) 中时,如何从 AndroidLauncher 获取“AdMob 类型”变量?

例子:安卓模块

广告管理器

package com.game.mygame.monetization;

public interface AdsManager {
void showAds(boolean show);
}

安卓启动器

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 时,它无法解析私有(private) 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 接口(interface)

我的方法是,每当你想用 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());
}
}

广告管理器

public interface AdsManager {
void implementAds();
void requestAds(); // Relative layout param removed from here
void showAds(boolean show);
}

在此之后,如果您希望这些操作影响您的游戏,只需将您的游戏实例(核心模块)的引用从 AndroidLauncher 传递给 AndroidAdsManager 即可能够从那里调用您想要的任何方法,希望这对您有所帮助!

关于java - LibGDX 从核心模块 (MyGdxGame) 中的 android 模块 (AndroidLauncher) 获取变量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51341235/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com