Android 实施Admob。一个活动的游戏

Android 实施Admob。一个活动的游戏,android,admob,Android,Admob,我一直在学习本教程:,我已经完成了游戏,现在我想添加AdMob。我设法在AndroidGame类中添加了Intersicial广告(AndroidGame类和其他类的代码可以在上面提到的教程中找到)。调用onResume()方法时显示Ad。那不是我真正需要的东西。我认为多看一些广告会更好。当游戏暂停或在游戏屏幕上显示Intersicial或banner会很好 最近我发现: 这是我需要的东西,但它不起作用。我试图修复它,但没有结果。我的问题是:如何修复此代码?还是有更好的方式添加广告 我的代码是一

我一直在学习本教程:,我已经完成了游戏,现在我想添加AdMob。我设法在AndroidGame类中添加了Intersicial广告(AndroidGame类和其他类的代码可以在上面提到的教程中找到)。调用onResume()方法时显示Ad。那不是我真正需要的东西。我认为多看一些广告会更好。当游戏暂停或在游戏屏幕上显示Intersicial或banner会很好

最近我发现:

这是我需要的东西,但它不起作用。我试图修复它,但没有结果。我的问题是:如何修复此代码?还是有更好的方式添加广告

我的代码是一样的,所以在我看来发布它毫无意义。如果需要的话,我会把它贴出来

编辑:

AdMobHandler

import com.google.android.gms.ads.AdView;
import android.os.Handler;
import android.os.Message;
public class AdMobHandler extends Handler {
private AdView adView=null;public AdMobHandler(AdView adView) {
super();
this.adView = adView;
}@Override
public void handleMessage(Message msg) {
if ( adView.getVisibility() != msg.what ) {
adView.setVisibility(msg.what);
}
}
}
机器人游戏

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.kilobolt.robotgame.AdMobHandler;
import com.kilobolt.helper.FileIO;
import com.kilobolt.helper.Game;
import com.kilobolt.helper.Graphics;public abstract class AndroidGame extends Activity implements Game {
public static AdRequest adRequest;
public static AdRequest adRequest2;
public static AdMobHandler admobHandler;
public static AdMobHandler admobHandler2;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);vib = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
int frameBufferWidth = isPortrait ? 480 : 800;
int frameBufferHeight = isPortrait ? 800 : 480;
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth,
frameBufferHeight, Config.RGB_565);

float scaleX = (float) frameBufferWidth
/ getWindowManager().getDefaultDisplay().getWidth();

float scaleY = (float) frameBufferHeight
/ getWindowManager().getDefaultDisplay().getHeight();renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, scaleX, scaleY);
screen = getInitScreen();RelativeLayout layout = new RelativeLayout(this);
layout.addView(renderView);
adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("myid");
adView2 = new AdView(this);
adView2.setAdSize(AdSize.BANNER);
adView2.setAdUnitId("myid");
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(adView, params);
params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
layout.addView(adView2, params);setContentView(layout);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
adView2.loadAd(adRequest);
AdMobHandler admobHandler = new AdMobHandler(adView);
AdMobHandler admobHandler2 = new AdMobHandler(adView2);
PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK,
"MyGame");
admobHandler.sendEmptyMessage(View.GONE);
admobHandler2.sendEmptyMessage(View.GONE);
}
@Override
public void setScreen(Screen screen) {
if (screen == null)
throw new IllegalArgumentException("Screen must not be null");
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
}

} 
游戏屏幕的一部分

import com.google.android.gms.ads.AdView;
public class GameScreen extends Screen {
public static AdMobHandler admobHandler;
public static AdMobHandler admobHandler2;
private AdView adView;
private AdView adView2;@Override
public void update(float deltaTime) {
List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

if (state == GameState.Ready) {
updateReady(touchEvents);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
if (state == GameState.Running) {
updateRunning(touchEvents, deltaTime);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
if (state == GameState.Paused) {
updatePaused(touchEvents);
AndroidGame.admobHandler.sendEmptyMessage(View.VISIBLE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
if (state == GameState.GameOver) {
updateGameOver(touchEvents);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
import com.google.android.gms.ads.AdView;
公共类游戏屏幕扩展屏幕{
公共静态AdMobHandler AdMobHandler;
公共静态AdMobHandler AdMobHandler 2;
私人咨询咨询;
私有AdView adView2;@Override
公共无效更新(浮动增量时间){
List touchEvents=game.getInput().getTouchEvents();
if(state==GameState.Ready){
更新日期(touchEvents);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
if(state==GameState.Running){
更新规划(touchEvents,deltaTime);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
如果(状态==游戏状态。暂停){
UpdatePause(触摸事件);
AndroidGame.admobHandler.sendEmptyMessage(View.VISIBLE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}
if(state==GameState.GameOver){
updateGameOver(触摸事件);
AndroidGame.admobHandler.sendEmptyMessage(View.GONE);
AndroidGame.admobHandler2.sendEmptyMessage(View.VISIBLE);
}

为了延长您的寿命,您需要发布它,因此只要此问题仍然存在,您的代码问题就仍然存在。同时也帮助您的回答者回答您的问题。您应该发布您的代码,因为链接的资源可能会消失、移动等。还可以帮助其他有类似问题的人找到它