我一直在关注本教程:http://www.kilobolt.com/day-6-the-android-game-framework-part-ii.html,我已完成游戏,现在我想添加AdMob。我设法在AndroidGame类中添加了Intersticial Ad(AndroidGame类的代码和其他类可以在上面提到的教程中找到)。调用onResume()方法时会显示广告。这不是我真正需要的东西。我认为更频繁地展示广告会更好。当游戏暂停或在Game Over屏幕上显示interticial或banner时会很不错。
所以最近我发现了这个:http://forum.kilobolt.com/viewtopic.php?f=11&t=65&p=528&hilit=admob#p528
这是我需要的东西,但它不起作用。我试图修复它没有结果。我的问题是:如何修复此代码?或者有更好的方法来添加广告吗?
我的代码是一样的,所以发布它在我看来毫无意义。如果需要,我会发布。
编辑:
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);
}
}
}
AndroidGame
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;
}
}
GameScreen的一部分
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);
}