在表面视图上显示AdMob广告

时间:2016-02-17 00:47:29

标签: java android admob

我刚刚开始编写我的第一款Android游戏,现在我也想尝试使用AdMob。但我不能让它显示广告...... 我按照本教程创建游戏:http://www.kilobolt.com/day-7-creating-an-android-game-from-start-to-finish.html 它使用画布绘制所有内容。

这是我猜的重要代码:

public abstract class AndroidGame extends Activity implements Game {
AndroidFastRenderView renderView;
Graphics graphics;
Audio audio;
Input input;
FileIO fileIO;
Screen screen;
WakeLock wakeLock;

AdView adView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);

    boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);

    int frameBufferWidth = metrics.widthPixels;
    int frameBufferHeight = metrics.heightPixels;

    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();
    //setContentView(renderView);
    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");

    RelativeLayout layout = new RelativeLayout(this);
    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT,
            RelativeLayout.LayoutParams.MATCH_PARENT);
    layout.setLayoutParams(params);

    adView = createAdView();
    layout.addView(adView);

    View gameView =renderView;
    layout.addView(gameView);

    setContentView(layout);
    startAdvertising(adView);
}

private AdView createAdView() {
    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-number/number");

    adView.setId(12345);

    RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
    params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
    params.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);

    adView.setLayoutParams(params);
    adView.setBackgroundColor(Color.BLACK);

    return adView;
}


private void startAdvertising(AdView adView) {
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

我做错了什么?

更新 使用“Ads”作为logcat中的过滤器。 你需要使用这样的测试设备:

private void startAdvertising(AdView adView) {
    AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice("your ID")
            .build();
    adView.loadAd(adRequest);
}

我将adUnitId更改为此处给出的内容:https://developers.google.com/admob/android/quick-start 然后我改变了添加Views的顺序(看起来gameView覆盖了adView):

    View gameView =renderView;
    layout.addView(gameView);

    adView = createAdView();
    layout.addView(adView);

通过这些改变,它起作用了。

0 个答案:

没有答案