我想在我的游戏中添加插页式广告.. 每场比赛结束后都会出现插页式广告。 和代码形成其他开发人员,我已切换Admobs.jar库来播放服务广告.. 当游戏结束时广告没有出现...... 在logcat中,它说广告已完成加载。
3-18 21:14:50.226 32551-32551/com.mogames.beachguard E/MediaPlayer: Should have subtitle controller already set
03-18 21:14:51.581 32551-32551/com.mogames.beachguard E/MediaPlayer: Should have subtitle controller already set
03-18 21:14:59.966 32551-32551/com.mogames.beachguard E/MediaPlayer: Should have subtitle controller already set
03-18 21:15:07.791 32551-32551/com.mogames.beachguard I/dalvikvm: Could not find method android.security.NetworkSecurityPolicy.getInstance, referenced from method com.google.android.gms.ads.internal.state.f.a
03-18 21:15:07.996 32551-32551/com.mogames.beachguard I/dalvikvm: Could not find method android.security.NetworkSecurityPolicy.getInstance, referenced from method com.google.android.gms.ads.internal.state.f.a
03-18 21:15:08.346 32551-32551/com.mogames.beachguard I/Ads: Starting ad request.
03-18 21:15:08.366 32551-32551/com.mogames.beachguard W/Ads: The interstitial has not loaded.
03-18 21:15:09.471 32551-32551/com.mogames.beachguard I/dalvikvm: Could not find method android.webkit.WebSettings.setMixedContentMode, referenced from method com.google.android.gms.ads.internal.webview.o.<init>
03-18 21:15:09.496 32551-32551/com.mogames.beachguard I/chromium: [INFO:library_loader_hooks.cc(112)] Chromium logging enabled: level = 0, default verbosity = 0
03-18 21:15:09.501 32551-32551/com.mogames.beachguard I/chromium: [INFO:url_util.cc(429)] AddStandardScheme is not supported
03-18 21:15:09.501 32551-32551/com.mogames.beachguard I/chromium: [INFO:url_util.cc(429)] AddStandardScheme is not supported
03-18 21:15:09.501 32551-32551/com.mogames.beachguard I/chromium: [INFO:url_util.cc(429)] AddStandardScheme is not supported
03-18 21:15:09.501 32551-32551/com.mogames.beachguard I/chromium: [INFO:url_util.cc(429)] AddStandardScheme is not supported
03-18 21:15:11.161 32551-32551/com.mogames.beachguard I/Ads: Ad finished loading.
我的Java代码是
package com.mogames.beachguard;
public class MainGame extends Screen {
//ad
private com.google.android.gms.ads.InterstitialAd interstitial;
int ad_counter = 0;
//game over counter
int gameover_counter = 0;
boolean game_over = false;
//TODO: variables you can change to control game speed, delays...
int gameover_delay = 20;
int sand_height = 50;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//setDebugMode(true);
//initialiseAccelerometer();
//highscores
highscoreManager = new HighScoreManager(this, savedInstanceState, layout);
// Create the interstitial
interstitial = new com.google.android.gms.ads.InterstitialAd(this );
interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));
}
com.google.android.gms.ads.AdRequest adRequest;
public void openAd() {
runOnUiThread(new Runnable() {
public void run() {
// Create ad request
adRequest = new com.google.android.gms.ads.AdRequest.Builder()
.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("C7929CF76D7E393618FEA6C0F97D941F")
.addTestDevice("3063E9F211291FEBEC7210C5CD24D4C4")
.build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(new com.google.android.gms.ads.AdListener() {
@Override
public void onAdClosed() {
}
}
);
interstitial.show();
}
});
}
public synchronized void GameOver() {
if (ad_counter >= getResources().getInteger(R.integer.add_shows_every_X_gameovers)) {
openAd();//TODO: Remove openAd(); to stop interstatial ads.
ad_counter = 0;
}
ad_counter++;
StopMusic();
state = GAMEOVER;
highscoreManager.AddName_Editview(((int) (ScreenWidth() / 1.5f) < dpToPx(250)) ? ((int) (ScreenWidth() / 1.5f)) : (dpToPx(250)), getResources().getString(R.string.Default_topscore_name), (int) (ScreenHeight() * 0.68f));
}
public void OpenHighscores() {
state = HIGHSCORES;
highscore_list = highscoreManager.load_localscores();
}
public void createBubble(float y) {
if (Math.random() > 0.5)
bubbles.add(new Instance(bubble_sprite, (float) ((Math.random() * ScreenWidth()) - (bubble_sprite.getWidth() / 2)), y, this, false));
else
bubbles.add(new Instance(bubble_sprite2, (float) ((Math.random() * ScreenWidth()) - (bubble_sprite2.getWidth() / 2)), y, this, false));
}
public void PlayMusic() {
if (!music_muted && state == GAMEPLAY) {
music = MediaPlayer.create(activity, R.raw.music);
music.setVolume(0.4f, 0.4f);
music.start();
music.setLooping(true);
}
}
public void StopMusic() {
if (music != null)
music.stop();
}
public void toggleMusic() {
if (music_muted) {
music_muted = false;
btn_music_mute.sprite = music_on;
if (!pause) {
PlayMusic();
}
} else {
music_muted = true;
btn_music_mute.sprite = music_off;
StopMusic();
}
}
public void toggleSoundFx() {
if (sound_muted) {
sound_muted = false;
btn_sound_mute.sprite = sound_on;
} else {
sound_muted = true;
btn_sound_mute.sprite = sound_off;
}
}
public void pause() {
if (state == GAMEPLAY) {
pause = true;
StopMusic();
btn_pause.sprite = play_btn_sprite;
}
}
public void unPause() {
pause = false;
btn_pause.sprite = pause_btn_sprite;
if (!music_muted)
PlayMusic();
}
public void togglePause() {
if (state == GAMEPLAY) {
if (pause)
unPause();
else
pause();
}
}
public void Rate() {
Uri uri = Uri.parse("market://details?id=" + getPackageName());
Intent goToMarket = new Intent(Intent.ACTION_VIEW, uri);
try {
startActivity(goToMarket);
} catch (ActivityNotFoundException e) {
Toast.makeText(this, getResources().getString(R.string.unable_to_reach_market), Toast.LENGTH_LONG).show();
}
}
//...................................................Rendering of screen............................................................................................................................
@Override
public void Draw(Canvas canvas) {
//draw background
renderBackground(canvas);
if (state == MENU) {
//draw bubbles
bubble_sprite2.draw(canvas, dpToPx(15), dpToPx(50));
bubble_sprite.draw(canvas, ScreenWidth() - (bubble_sprite.getWidth()), -(bubble_sprite.getHeight() * 0.3f));
//draw sand
drawSand(canvas);
//draw buttons
btn_Highscores.draw(canvas);
btn_Exit.draw(canvas);
btn_Play.draw(canvas);
btn_rate.draw(canvas);
//draw grass and bottle
grass_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
bottle_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - bottle_sprite.getHeight() - dpToPx(sand_height) * 0.6f);
grass_sprite.draw(canvas, ScreenWidth() * 0.4f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
grass_sprite.draw(canvas, ScreenWidth() * 0.6f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.3f);
grass_sprite.draw(canvas, ScreenWidth() * 0.8f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.2f);
canvas.drawText(getResources().getString(R.string.app_name), (ScreenWidth() / 2) - (Title_Paint.measureText(getResources().getString(R.string.app_name)) / 2), (float) (ScreenHeight() * 0.25), Title_Paint);
} else if (state == GAMEPLAY) {
//draw bubbles
for (int i = 0; i < bubbles.size(); i++) {
bubbles.get(i).draw(canvas);
}
//draw sand
drawSand(canvas);
//draw beach sign
beach_sprite.draw(canvas, beach_sprite.getWidth() * 0.2f, ScreenHeight() - beach_sprite.getHeight() - dpToPx(sand_height) * 0.6f);
//draw grass and bottle
grass_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
bottle_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - bottle_sprite.getHeight() - dpToPx(sand_height) * 0.6f);
grass_sprite.draw(canvas, ScreenWidth() * 0.4f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
grass_sprite.draw(canvas, ScreenWidth() * 0.6f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.3f);
grass_sprite.draw(canvas, ScreenWidth() * 0.8f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.2f);
fishManager.drawObjects(canvas);
//draw score
canvas.drawText("" + score, (ScreenWidth() * 0.5f) - (Title_Paint.measureText("" + score) / 2), (float) (ScreenHeight() * 0.35f), Score_Paint);
//before game starts
if (notstarted) {
canvas.drawText(getResources().getString(R.string.Tap_to_start), (ScreenWidth() / 2) - (Instruction_Paint.measureText(getResources().getString(R.string.Tap_to_start)) / 2), (float) (ScreenHeight() * 0.5), Instruction_Paint);
canvas.drawText(getResources().getString(R.string.Tap_to_start2), (ScreenWidth() / 2) - (Instruction_Paint.measureText(getResources().getString(R.string.Tap_to_start2)) / 2), (float) (ScreenHeight() * 0.6), Instruction_Paint);
} else if (pause) {
canvas.drawText(getResources().getString(R.string.Paused), (ScreenWidth() / 2) - (Instruction_Paint.measureText(getResources().getString(R.string.Paused)) / 2), (float) (ScreenHeight() * 0.5), Instruction_Paint);
}
//pause button
btn_pause.draw(canvas);
} else if (state == HIGHSCORES) {
//draw bubbles
bubble_sprite2.draw(canvas, dpToPx(15), dpToPx(50));
bubble_sprite.draw(canvas, ScreenWidth() - (bubble_sprite.getWidth()), -(bubble_sprite.getHeight() * 0.3f));
//draw sand
drawSand(canvas);
//draw buttons
btn_Home.draw(canvas);
//draw grass and bottle
grass_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
bottle_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - bottle_sprite.getHeight() - dpToPx(sand_height) * 0.6f);
grass_sprite.draw(canvas, ScreenWidth() * 0.4f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
grass_sprite.draw(canvas, ScreenWidth() * 0.6f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.3f);
grass_sprite.draw(canvas, ScreenWidth() * 0.8f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.2f);
canvas.drawText(getResources().getString(R.string.Highscores), (ScreenWidth() / 2) - (Title_Paint.measureText(getResources().getString(R.string.Highscores)) / 2), (float) (ScreenHeight() * 0.25), Title_Paint);
if (highscore_list != null) {
//hiscores
for (int i = 0; i < highscore_list.length; i++) {
canvas.drawText(highscore_list[i].hiscorename, (ScreenWidth() / 2) - (ScreenWidth() / 4), (ScreenHeight() * 0.35f) + (i * SubTitle_Paint.getTextSize() * 1.5f), SubTitle_Paint);
canvas.drawText("" + highscore_list[i].highscore, (ScreenWidth() / 2) + (ScreenWidth() / 6), (ScreenHeight() * 0.35f) + (i * SubTitle_Paint.getTextSize() * 1.5f), SubTitle_Paint);
}
}
} else if (state == GAMEOVER) {
//draw bubbles
bubble_sprite2.draw(canvas, dpToPx(15), dpToPx(50));
bubble_sprite.draw(canvas, ScreenWidth() - (bubble_sprite.getWidth()), -(bubble_sprite.getHeight() * 0.3f));
//draw sand
drawSand(canvas);
//draw buttons
btn_facebook.draw(canvas);
btn_Home.draw(canvas);
btn_Replay.draw(canvas);
//draw grass and bottle
grass_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
bottle_sprite.draw(canvas, ScreenWidth() * 0.1f, ScreenHeight() - bottle_sprite.getHeight() - dpToPx(sand_height) * 0.6f);
grass_sprite.draw(canvas, ScreenWidth() * 0.4f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.5f);
grass_sprite.draw(canvas, ScreenWidth() * 0.6f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.3f);
grass_sprite.draw(canvas, ScreenWidth() * 0.8f, ScreenHeight() - grass_sprite.getHeight() - dpToPx(sand_height) * 0.2f);
//draw text
canvas.drawText(getResources().getString(R.string.game_over), (ScreenWidth() / 2) - (Title_Paint.measureText(getResources().getString(R.string.game_over)) / 2), (float) (ScreenHeight() * 0.25), Title_Paint);
canvas.drawText("" + score, (ScreenWidth() / 2) - (Score_Paint.measureText("" + score) / 2), (float) (ScreenHeight() * 0.45), Score_Paint);
canvas.drawText(getResources().getString(R.string.Enter_highscore_comment), (ScreenWidth() / 2) - (SubTitle_Paint.measureText(getResources().getString(R.string.Enter_highscore_comment)) / 2), (float) (ScreenHeight() * 0.65), SubTitle_Paint);
}
//draw sound buttons
btn_sound_mute.draw(canvas);
btn_music_mute.draw(canvas);
//physics.drawDebug(canvas);
super.Draw(canvas);
}
//Rendering of background
public void renderBackground(Canvas canvas) {
//TODO: you may wish to change background colors from here
//canvas.drawRect(0, 0, ScreenWidth(), ScreenHeight(), background_shader);
canvas.drawColor(Color.rgb(38, 148, 191));
}
public void drawSand(Canvas canvas) {
Path sand_path = new Path();
sand_path.reset(); // only needed when reusing this path for a new build
sand_path.moveTo(0, ScreenHeight() - dpToPx(sand_height)); // used for first point
sand_path.lineTo(ScreenWidth(), ScreenHeight() - dpToPx(sand_height) * 0.7f);
sand_path.lineTo(ScreenWidth(), ScreenHeight());
sand_path.lineTo(0, ScreenHeight());
canvas.drawPath(sand_path, Sand_shader);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
highscoreManager.onActivityResult(requestCode, resultCode, data);
}
@Override
protected void onResume() {
super.onResume();
highscoreManager.onResume();
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
highscoreManager.onSaveInstanceState(outState);
}
@Override
public void onPause() {
super.onPause();
pause();
highscoreManager.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
highscoreManager.onDestroy();
}
}
和gardle
dependencies {
compile project(':facebookSDK')
compile project(':facebookSDK')
compile 'com.google.firebase:firebase-ads:10.2.0'
compile 'com.google.firebase:firebase-core:10.2.0'
compile 'com.google.android.gms:play-services-ads:10.2.0'
}
apply plugin: 'com.google.gms.google-services'
项目gardle
dependencies {
classpath 'com.android.tools.build:gradle:2.3.0'
classpath 'com.google.gms:google-services:3.0.0'
}
并且游戏没有主要的Xml布局
答案 0 :(得分:1)
您在加载广告后立即展示广告。强烈建议尽早致电#loadAd()
(例如,在您的活动的onCreate方法中)以允许预加载广告。
在onCreate方法中加载广告:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//setDebugMode(true);
//initialiseAccelerometer();
//highscores
highscoreManager = new HighScoreManager(this, savedInstanceState, layout);
// Create the interstitial
interstitial = new com.google.android.gms.ads.InterstitialAd(this );
interstitial.setAdUnitId(getResources().getString(R.string.InterstitialAd_unit_id));
// Create ad request
adRequest = new com.google.android.gms.ads.AdRequest.Builder()
.addTestDevice(com.google.android.gms.ads.AdRequest.DEVICE_ID_EMULATOR)
// .addTestDevice("C7929CF76D7E393618FEA6C0F97D941F")
.addTestDevice("3063E9F211291FEBEC7210C5CD24D4C4")
.build();
// Begin loading your interstitial
interstitial.loadAd(adRequest);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(new com.google.android.gms.ads.AdListener() {
@Override
public void onAdClosed() {
}
}
);
}
在您的#openAd()
方法中,展示广告:
public void openAd() {
runOnUiThread(new Runnable() {
public void run() {
if (interstitial.isLoaded())
interstitial.show();
}
});
}