如何让 AdMob 和 LibGdx 游戏浏览量在 Android 上发挥作用
How to get the AdMob and LibGdx game Views to work on Android
我最终使用了来自以下网站的大部分 AdMob 横幅示例代码:
https://github.com/googleads/googleads-mobile-android-examples/tree/master/java/admob/BannerExample
连同“扩展 Android应用程序”的其他示例源的部分内容
Android 启动器。
它建立了,从 Listener onAdLoaded() 消息来看,广告似乎
正在加载,但我的游戏屏幕总是占满整个屏幕。
我曾尝试减小游戏视口的大小,但没有成功。
游戏的代码没有使用Scene2D,所以没有使用Stage和Actor,
只有视口和相机。
这仅用于 LibGdx Android,使用 Windows 进行开发。
我该怎么做才能让这两个视图正确显示并显示游戏画面
和广告?
我尝试过使用各种不同的代码变体,
和最新的播放服务广告版本,
但最终结果是一样的。
谢谢!!
代码如下:
dependencies {
implementation 'com.google.android.gms:play-services-ads:19.6.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jps.gamenew" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:label="@string/app_name" >
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity
android:name="G_GameNewActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.jps.gamenew;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdListener;
import android.util.Log;
import java.util.Arrays;
import java.lang.Object;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
/*
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
/**/
public class G_GameNewActivity extends AndroidApplication
{
static final String TAG = "G_GameNewActivity";
private static final String BANNER_AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
protected AdView adView;
protected View gameView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
setContentView(layout);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener()
{
@Override
public void onInitializationComplete(InitializationStatus initializationStatus)
{
}
});
//I/Ads ( 7493): Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
//to get test ads on this device.
// Set your test devices. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device."
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
.build());
AdView adView = createAdView();
layout.addView(adView);
adView.setAdListener(new AdListener()
{
@ Override
public void onAdOpened()
{
Log.d(TAG, "\nonAdOpened()");
}
@ Override
public void onAdLoaded()
{
Log.d(TAG, "\nonAdLoaded()");
}
});
View gameView = createGameView(cfg);
layout.addView(gameView);
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
private AdView createAdView()
{
Log.d(TAG, "\n\ncreateAdView()");
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
//adView.setAdUnitId(AD_UNIT_ID);
adView.setAdUnitId(BANNER_AD_UNIT_ID); // "ca-app-pub-3940256099942544/6300978111"
//adView.setAdUnitId("/6499/example/banner");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg)
{
gameView = initializeForView(new GameTest(), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
@Override
public void onResume() {
super.onResume();
if (adView != null)
adView.resume();
}
@Override
public void onPause() {
if (adView != null)
adView.pause();
super.onPause();
}
@Override
public void onDestroy() {
if (adView != null)
adView.destroy();
super.onDestroy();
}
}
像这样制作 XML 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/game_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ad_view"
android:layout_alignParentTop="true" />
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="BANNER"
app:adUnitId="@string/banner_ad_unit_id" />
</RelativeLayout>
然后在您的 activity 中初始化视图,并将视图添加到 FrameLayout 容器中。我还删除了您使用 createGameView
方法的方式,因为它非常混乱地处理成员变量、局部变量和 return 值。此外,您忘记将 AdView 实例分配给成员变量。您改为将它分配给局部变量,因此生命周期事件不会传递给它。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.my_layout)
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener()
{
@Override
public void onInitializationComplete(InitializationStatus initializationStatus)
{
}
});
//I/Ads ( 7493): Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
//to get test ads on this device.
// Set your test devices. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device."
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
.build());
adView = findViewById(R.id.ad_view)
adView.setAdListener(new AdListener()
{
@ Override
public void onAdOpened()
{
Log.d(TAG, "\nonAdOpened()");
}
@ Override
public void onAdLoaded()
{
Log.d(TAG, "\nonAdLoaded()");
}
});
gameView = initializeForView(new GameTest(), cfg);
findViewById<FrameLayout>(R.id.game_container).addView(gameView);
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
我最终使用了来自以下网站的大部分 AdMob 横幅示例代码:
https://github.com/googleads/googleads-mobile-android-examples/tree/master/java/admob/BannerExample
连同“扩展 Android应用程序”的其他示例源的部分内容 Android 启动器。
它建立了,从 Listener onAdLoaded() 消息来看,广告似乎 正在加载,但我的游戏屏幕总是占满整个屏幕。
我曾尝试减小游戏视口的大小,但没有成功。
游戏的代码没有使用Scene2D,所以没有使用Stage和Actor, 只有视口和相机。
这仅用于 LibGdx Android,使用 Windows 进行开发。
我该怎么做才能让这两个视图正确显示并显示游戏画面 和广告?
我尝试过使用各种不同的代码变体, 和最新的播放服务广告版本, 但最终结果是一样的。
谢谢!!
代码如下:
dependencies {
implementation 'com.google.android.gms:play-services-ads:19.6.0'
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jps.gamenew" >
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:label="@string/app_name" >
<!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity
android:name="G_GameNewActivity"
android:label="@string/app_name"
android:screenOrientation="landscape"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
package com.jps.gamenew;
import android.content.Intent;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import com.badlogic.gdx.backends.android.AndroidApplication;
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdListener;
import android.util.Log;
import java.util.Arrays;
import java.lang.Object;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
/*
import com.google.android.gms.ads.admanager.AdManagerAdRequest;
import com.google.android.gms.ads.admanager.AdManagerAdView;
/**/
public class G_GameNewActivity extends AndroidApplication
{
static final String TAG = "G_GameNewActivity";
private static final String BANNER_AD_UNIT_ID = "ca-app-pub-3940256099942544/6300978111";
protected AdView adView;
protected View gameView;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
layout.setLayoutParams(params);
setContentView(layout);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener()
{
@Override
public void onInitializationComplete(InitializationStatus initializationStatus)
{
}
});
//I/Ads ( 7493): Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
//to get test ads on this device.
// Set your test devices. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device."
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
.build());
AdView adView = createAdView();
layout.addView(adView);
adView.setAdListener(new AdListener()
{
@ Override
public void onAdOpened()
{
Log.d(TAG, "\nonAdOpened()");
}
@ Override
public void onAdLoaded()
{
Log.d(TAG, "\nonAdLoaded()");
}
});
View gameView = createGameView(cfg);
layout.addView(gameView);
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
private AdView createAdView()
{
Log.d(TAG, "\n\ncreateAdView()");
adView = new AdView(this);
adView.setAdSize(AdSize.SMART_BANNER);
//adView.setAdUnitId(AD_UNIT_ID);
adView.setAdUnitId(BANNER_AD_UNIT_ID); // "ca-app-pub-3940256099942544/6300978111"
//adView.setAdUnitId("/6499/example/banner");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
adView.setLayoutParams(params);
adView.setBackgroundColor(Color.BLACK);
return adView;
}
private View createGameView(AndroidApplicationConfiguration cfg)
{
gameView = initializeForView(new GameTest(), cfg);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
params.addRule(RelativeLayout.BELOW, adView.getId());
gameView.setLayoutParams(params);
return gameView;
}
@Override
public void onResume() {
super.onResume();
if (adView != null)
adView.resume();
}
@Override
public void onPause() {
if (adView != null)
adView.pause();
super.onPause();
}
@Override
public void onDestroy() {
if (adView != null)
adView.destroy();
super.onDestroy();
}
}
像这样制作 XML 布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/game_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="@+id/ad_view"
android:layout_alignParentTop="true" />
<com.google.android.gms.ads.AdView
android:id="@+id/ad_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
app:adSize="BANNER"
app:adUnitId="@string/banner_ad_unit_id" />
</RelativeLayout>
然后在您的 activity 中初始化视图,并将视图添加到 FrameLayout 容器中。我还删除了您使用 createGameView
方法的方式,因为它非常混乱地处理成员变量、局部变量和 return 值。此外,您忘记将 AdView 实例分配给成员变量。您改为将它分配给局部变量,因此生命周期事件不会传递给它。
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration();
cfg.useAccelerometer = false;
cfg.useCompass = false;
// Do the stuff that initialize() would do for you
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
setContentView(R.layout.my_layout)
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener()
{
@Override
public void onInitializationComplete(InitializationStatus initializationStatus)
{
}
});
//I/Ads ( 7493): Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
//to get test ads on this device.
// Set your test devices. Check your logcat output for the hashed device ID to
// get test ads on a physical device. e.g.
// "Use RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("ABCDEF012345"))
// to get test ads on this device."
MobileAds.setRequestConfiguration(
new RequestConfiguration.Builder().setTestDeviceIds(Arrays.asList("723B283FA24BF57D2A1BA9A7C34BDF6C"))
.build());
adView = findViewById(R.id.ad_view)
adView.setAdListener(new AdListener()
{
@ Override
public void onAdOpened()
{
Log.d(TAG, "\nonAdOpened()");
}
@ Override
public void onAdLoaded()
{
Log.d(TAG, "\nonAdLoaded()");
}
});
gameView = initializeForView(new GameTest(), cfg);
findViewById<FrameLayout>(R.id.game_container).addView(gameView);
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().build();
// Start loading the ad in the background.
adView.loadAd(adRequest);
}