我在Android游戏中进行编辑并且它没有主XML,我想在屏幕上放置一个插页式广告...开发人员说将单位广告ID放在String.Xml中但是没有显示任何AD 我为广告推出了Firebase Stuff,但我遇到了这个错误
Error:(125, 25) error: AdRequest() has private access in AdRequest
这是我的主要java:
package com.mogames.beachguard;
import java.util.ArrayList;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Shader;
import android.graphics.Typeface;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.media.SoundPool;
import android.net.Uri;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.Toast;
import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.InterstitialAd;
import com.google.ads.AdRequest.ErrorCode;
import com.mogames.nudge.Button;
import com.mogames.nudge.HighScoreManager;
import com.mogames.nudge.Instance;
import com.mogames.nudge.ObjectManager;
import com.mogames.nudge.Physics;
import com.mogames.nudge.Screen;
import com.mogames.nudge.Sprite;
public class MainGame extends Screen {
//paints
Paint background_shader = new Paint();
Paint Title_Paint = new Paint();
Paint SubTitle_Paint = new Paint();
Paint Score_Paint = new Paint();
Paint Instruction_Paint = new Paint();
Paint Sand_shader = new Paint();
//background
Bitmap background;
//instances
ArrayList<Instance> bubbles = new ArrayList<Instance>();
Sprite bubble_sprite, bubble_sprite2;
//physics
Physics physics = new Physics();
//states
final int MENU = 0, GAMEPLAY = 1, HIGHSCORES = 2, GAMEOVER = 3;
int state = MENU;
boolean pause = false, notstarted = true;
//menu buttons
Button btn_Play, btn_Highscores, btn_Exit, btn_Home, btn_facebook, btn_Replay, btn_sound_mute, btn_music_mute, btn_pause, btn_rate;
Sprite play_btn_sprite, pause_btn_sprite, beach_sprite, bottle_sprite, grass_sprite;
//score
int score = 0;
HighScoreManager highscoreManager;
HighScoreManager.Highscore[] highscore_list;
Sprite score_cup;
//sound
SoundPool sp;
MediaPlayer music;
int sound_beep, sound_bubble, sound_gameover;
boolean sound_muted = false, music_muted = false;
Sprite sound_on, sound_off, music_on, music_off;
//Colors
//TODO: Feel free to change these colors
final int BLACK = Color.argb(255, 51, 51, 51);
final int RED = Color.argb(255, 255, 112, 80);
final int WHITE = Color.argb(255, 255, 255, 255);
final int YELLOW = Color.argb(255, 255, 237, 90);
final int PEACH = Color.argb(255, 204, 196, 168);//sand color
//ad
private 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;
//Fish
ObjectManager fishManager;
final int JELLYFISH = 0, SPICKY = 1, PIRANHA = 2, SWORDY = 3, SHARK = 4;
@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 InterstitialAd(this, getResources().getString(R.string.InterstitialAd_unit_id));
}
public void openAd() {
runOnUiThread(new Runnable() {
public void run() {
// Create ad request
AdRequest request = new AdRequest();
// Begin loading your interstitial
interstitial.loadAd(request);
// Set Ad Listener to use the callbacks below
interstitial.setAdListener(new AdListener() {
@Override
public void onReceiveAd(Ad arg0) {
if (interstitial.isReady()) {
interstitial.show();
}
}
@Override
public void onPresentScreen(Ad arg0) {
}
@Override
public void onLeaveApplication(Ad arg0) {
}
@Override
public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
}
@Override
public void onDismissScreen(Ad arg0) {
}
});
}
});
}
......................
这是我的屏幕java代码:
package com.mogames.nudge;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Display;
import android.view.DragEvent;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnDragListener;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnTouchListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class Screen extends Activity implements Runnable, OnTouchListener, SensorEventListener {
private SurfaceHolder holder;
private boolean locker = true, initialised = false;
private Thread thread;
//public WakeLock WL;
private int width = 0, height = 0;
public float cameraX = 0, cameraY = 0;
public Activity activity = this;
public boolean debug_mode = false;
private long now = SystemClock.elapsedRealtime(), lastRefresh, lastfps;
public SurfaceView surface;
private int fps = 0, frames = 0, runtime = 0, drawtime = 0;
//sensor
SensorManager sm;
Sensor s;
float sensorx, calibratex = 0;
float sensory, calibratey = 0;
private boolean default_lanscape = false;
private int default_lanscape_rotation = 0;
//world origin
public final int TOP_LEFT = 0, BOTTOM_LEFT = 1;
public int origin = TOP_LEFT;
//layout
public RelativeLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
activity = this;
//full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
//create surface
layout = new RelativeLayout(this);
surface = new SurfaceView(this);
//TODO:MAKE SURE TO REMOVE INTERSTATIAL ADs BY GOING TO MAINGAME.JAVA LINE 595 AND REMOVING openAd();
//AdView ad = new AdView(this, AdSize.BANNER, "ca-app-pub-9058687372170937/9187964203"); //make sure id is of banner ad.
//layout
// RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// params1.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
// params1.addRule(RelativeLayout.CENTER_HORIZONTAL);
// ad.setLayoutParams(params1);
// layout.addView(ad);
// AdRequest request = new AdRequest();
// ad.loadAd(request);
//TODO :----------------------------------------------------------------------------------------------------------------------------------------------
layout.addView(surface);
setContentView(layout);
holder = surface.getHolder();
//listeners
surface.setOnTouchListener(this);
// start game loop
thread = new Thread(this);
thread.start();
onCreate();
}
/* Main game loop.......................................................... */
@Override
public void run() {
//int rand = (int) (Math.random() * 100);
synchronized (ACCESSIBILITY_SERVICE) {
while (locker) {
//System.out.println("start-");
now = SystemClock.elapsedRealtime();
if (now - lastRefresh > 37) {//limit 35fps - 28
lastRefresh = SystemClock.elapsedRealtime();
if (!holder.getSurface().isValid()) {
continue;
}
//fps
if (now - lastfps > 1000) {
fps = frames;
frames = 0;
lastfps = SystemClock.elapsedRealtime();
} else {
frames++;
}
//step
if (initialised)
Step();
//take run time
runtime = (int) (SystemClock.elapsedRealtime() - lastRefresh);
//draw screen
Canvas canvas = holder.lockCanvas();
if (initialised)
Draw(canvas);
else {
//initialise game
width = canvas.getWidth();
height = canvas.getHeight();
Start();
initialised = true;
}
holder.unlockCanvasAndPost(canvas);
//take render time
drawtime = (int) (SystemClock.elapsedRealtime() - lastRefresh) - runtime;
}
//System.out.println("finish-----");
//try {
// Thread.sleep(10);
//} catch (InterruptedException e) {
// e.printStackTrace();
//}
}
}
}
/* Detect and override back press */
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
super.onKeyDown(keyCode, event);
if (keyCode == KeyEvent.KEYCODE_BACK) {
BackPressed();
return false;
}
return false;
}
/* Events.................................................................. */
public void onCreate() {
}
public void Start() {
}
synchronized public void Step() {
}
public void Draw(Canvas canvas) {
if (debug_mode) {
Paint paint = new Paint();
paint.setColor(Color.BLACK);
paint.setTextSize(dpToPx(20));
canvas.drawText("Width: " + width + ", Height: " + height, 5, dpToPx(20), paint);
canvas.drawText("default landscape: " + default_lanscape + " Rotation: " + default_lanscape_rotation, 5, 5 + dpToPx(20) * 2, paint);
canvas.drawText("FPS: " + fps + "run_time: " + runtime + "draw_time: " + drawtime, 5, 5 + dpToPx(20) * 3, paint);
}
}
public void Finish() {
}
public void Pause() {
locker = false;
while (true) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
}
public void Resume() {
locker = true;
thread = new Thread(this);
thread.start();
}
public synchronized void BackPressed() {
}
public synchronized void onTouch(float TouchX, float TouchY, MotionEvent event) {
}
public synchronized void onAccelerometer(PointF point) {
}
/* Functions............................................................... */
public void Exit() {
locker = false;
while (true) {
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
break;
}
thread = null;
System.exit(0);
activity.finish();
}
public Activity getActivity() {
return activity;
}
public void setDebugMode(boolean debugModeOn) {
debug_mode = debugModeOn;
}
//screen related
public int ScreenWidth() {
return width;
}
public int ScreenHeight() {
return height;
}
/**
* World X to Screen X
*
* @param worldX
* The x-coordinate relative to the world
*/
public int ScreenX(float worldX) {
return (int) (worldX - cameraX);
}
/**
* World Y to Screen Y
*
* @param worldY
* The Y-coordinate relative to the world
*/
public int ScreenY(float worldY) {
if (origin == TOP_LEFT)
return (int) (worldY - cameraY);
else
return ScreenHeight() - (int) (worldY - cameraY);
}
/**
* World origin (0,0)
*
* @param origin
* TOP_LEFT or BOTTOM_LEFT
*/
public void setOrigin(int origin) {
this.origin = origin;
}
public boolean inScreen(float x, float y) {
return ((ScreenY(y) > 0 && ScreenY(y) < ScreenHeight()) && (ScreenX(x) > 0 && ScreenX(x) < ScreenWidth()));
}
public int dpToPx(int dp) {
float density = getApplicationContext().getResources().getDisplayMetrics().density;
return Math.round((float) dp * density);
}
//sensor related
public void initialiseAccelerometer() {
//device has its default landscape or portrait
Display display = ((WindowManager) activity.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int rotation = display.getRotation();
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
//portrait
if (rotation == Surface.ROTATION_0)
default_lanscape = false;
if (rotation == Surface.ROTATION_180)
default_lanscape = false;
if (rotation == Surface.ROTATION_90)
default_lanscape = true;
if (rotation == Surface.ROTATION_270)
default_lanscape = true;
} else {
//landscape
if (rotation == Surface.ROTATION_0)
default_lanscape = true;
if (rotation == Surface.ROTATION_180)
default_lanscape = true;
if (rotation == Surface.ROTATION_90)
default_lanscape = false;
if (rotation == Surface.ROTATION_270)
default_lanscape = false;
}
default_lanscape_rotation = rotation;
sm = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
if (sm.getSensorList(Sensor.TYPE_ACCELEROMETER).size() != 0) {
s = sm.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);
sm.registerListener(this, s, SensorManager.SENSOR_DELAY_NORMAL);
}
}
public void CalibrateAccelerometer() {
calibratex = sensorx * Math.abs(sensorx);
calibratey = sensory * Math.abs(sensory);
}
public PointF getAccelerometer() {
return new PointF((sensorx * Math.abs(sensorx) - calibratex), (sensory * Math.abs(sensory) - calibratey));
}
/* Touch events.......................................................... */
@Override
public boolean onTouch(View v, MotionEvent event) {
if (initialised) {
onTouch(event.getX(), event.getY(), event);
}
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
@Override
public void onSensorChanged(SensorEvent event) {
if (initialised) {
//read values
if (getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
if (default_lanscape) {
sensorx = -event.values[1];
sensory = -event.values[0];
} else {
sensory = event.values[1];
sensorx = -event.values[0];
}
} else {
if (default_lanscape) {
sensory = event.values[1];
sensorx = -event.values[0];
} else {
sensorx = event.values[1];
sensory = event.values[0];
}
}
//call accelerometer event
onAccelerometer(new PointF((sensorx - calibratex), (sensory - calibratey)));
}
//sleep for a while
try {
Thread.sleep(16);
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
/* pause, destroy, resume................................................ */
@Override
protected void onResume() {
super.onResume();
Resume();
}
@Override
protected void onPause() {
super.onPause();
Pause();
}
@Override
protected void onDestroy() {
super.onDestroy();
Finish();
}
}
这是我的String.xml:
<?xml version="1.0" encoding="utf-8"?>
<!-- TO MODIFY MOST OF THE COLORS AND VARIABLES OPEN MAINGAME.JAVA AND SEARCH FOR TODO TAGS. THEY INDICATE THINGS YOU NEED TO CHANGE -->
<!-- App Name -->
<string name="app_name">Beach Guard</string>
<!-- Game Over -->
<string name="game_over">Game Over</string>
<!-- Top scores menu -->
<string name="Highscores">Top Scores</string>
<string name="Enter_highscore_comment">Congrats! Enter your name</string>
<string name="Default_topscore_name">LifeGuard</string>
<!-- Game Play -->
<string name="Paused">Paused</string>
<string name="Tap_to_start">The Beach is Being Invaded.</string>
<string name="Tap_to_start2">Tap On All Fish Fast!</string>
<!-- Main Menu -->
<string name="Play">Play</string>
<!-- Facebook settings_________________________________________________________________________________________________________________ -->
<!-- Create a new app on facebook, give it the hash of this App and the package name, then put the id here -->
<string name="app_facebook_id">194556057705301</string>
<string name="Error_no_facebook_app_installed">Facebook not installed on your Device.</string>
<string name="facebook_share_description">Check out my score on Sea Defense!</string>
<string name="facebook_share_title">My Score: </string>
<string name="facebook_share_picture_url">https://farm8.staticflickr.com/7363/12801590414_6e08bfb9c8_o.png</string>
<string name="facebook_share_link">https://play.google.com/store/apps/details?id=com.mogames.beachguard</string>
<!-- Admob settings____________________________________________________________________________________________________________________ -->
<!-- Add unit id - you should get the interstatial ad unit id from admob -->
<string name="InterstitialAd_unit_id">ca-app-pub-9058687372170937/8167059404</string>
<!-- The add can be shown once every 2 gameovers. replace with 1 to show it after every gameover -->
<integer name="add_shows_every_X_gameovers">1</integer>
<!-- Rate button settings__________________________________________________________________________________________________ -->
<!-- link is created automatically from package. Make sure package id is the same as the one used in google play -->
<string name="unable_to_reach_market">Error reaching market. Please check your internet connection</string>
所以任何人都可以帮助我在每场比赛结束后在游戏中放入一个插页式广告
答案 0 :(得分:1)
试试这个:
private InterstitialAd interstitial;
//Create the interstitial Ad.
interstitial = new InterstitialAd(this);
//Set unit id.
interstitial.setAdUnitId("ca-app-pub-3940256099942544/1033173712"); //test interstitial AD
//Create request.
AdRequest adRequest = new AdRequest.Builder().build();
//Start loading...
interstitial.loadAd(adRequest);
游戏停止的地方(GAME OVER)添加:
//Interstitial AD
if(interstitial.isLoaded()) {
interstitial.show();
}
else {
AdRequest interstitialRequest = new AdRequest.Builder().build();
interstitial.loadAd(interstitialRequest);
}
希望它有所帮助!