Android AdMob without XML

故事扮演 提交于 2019-12-31 04:20:08

问题


I have a massive question to ask as I am really stuck on this and it would be create to get ads on my free application, ok first off I have been following this book

Beginning Android games 2011

http://www.apress.com/9781430230427

Now this book implements a very nice and simple game framework which I use (a simpler version can be found here

http://www.kilobolt.com/day-5-the-android-game-framework-part-i.html

Now this framework doesn't use any type of XML file what so ever, it uses a framebuffer to draw things onto the screen. now when the application is first started, this is the first method called which is in the AndroidGame.java

@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;
    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();

    setContentView(renderView);

    PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
    wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame");
}

Where in here could i try and implement the admob banner? Also this is what a screen class looks like

public LogoScreen(Game game)
{
    super(game);
}

@Override
public void update(float deltaTime)
{
    Graphics g = game.getGraphics();
    List<TouchEvent> touchEvents = game.getInput().getTouchEvents();

    int len = touchEvents.size();

    for(int i = 0; i < len; i ++)
    {
        try
        {
            TouchEvent event = (TouchEvent) touchEvents.get(i);
            if(event.type == TouchEvent.TOUCH_DOWN)
            {
                game.setScreen(new MainMenuScreen(game));
            }
        }
        catch(IndexOutOfBoundsException io)
        {

        }
    }

}

@Override
public void paint(float deltaTime) 
{
    Graphics g = game.getGraphics();
    g.drawImage(Assets.logoScreen, 0, 0);
}

@Override
public void pause() {
    // TODO Auto-generated method stub

}

@Override
public void resume() {
    // TODO Auto-generated method stub

}

@Override
public void dispose() {
    // TODO Auto-generated method stub

}

@Override
public void backButton() 
{
    android.os.Process.killProcess(android.os.Process.myPid());
}

If I wanted to display an admob advert in the logoScreen what could i do that would work? I am really confused on how I can implement admob into my application, if any one can shine some light on this or help me that would be great :)

Thank you

Canvas

---Update--- Here is the code for the FastRenderView

package com.CC.framework.implementation;

//Imports
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class AndroidFastRenderView extends SurfaceView implements Runnable 
{
//Variables
AndroidGame game;
Bitmap framebuffer;
Thread renderThread = null;
SurfaceHolder holder;
volatile boolean running = false;

public AndroidFastRenderView(AndroidGame game, Bitmap framebuffer) 
{
    super(game);
    this.game = game;
    this.framebuffer = framebuffer;
    this.holder = getHolder();

}

public void resume() 
{ 
    running = true;
    renderThread = new Thread(this);
    renderThread.start();
}      

public void run() 
{
    Rect dstRect = new Rect();
    long startTime = System.nanoTime();
    while(running) 
    {  
        if(!holder.getSurface().isValid())
        {
            continue;           
        }

        float deltaTime = (System.nanoTime() - startTime) / 10000000.000f;
        startTime = System.nanoTime();

        if (deltaTime > 3.15)
        {
            deltaTime = (float) 3.15;
        }


        game.getCurrentScreen().update(deltaTime);
        game.getCurrentScreen().paint(deltaTime);

        Canvas canvas = holder.lockCanvas();
        canvas.getClipBounds(dstRect);
        canvas.drawBitmap(framebuffer, null, dstRect, null);                           
        holder.unlockCanvasAndPost(canvas);

    }
}

public void pause() 
{                        
    running = false;                        
    while(true) 
    {
        try 
        {
            renderThread.join();
            break;
        } 
        catch (InterruptedException e) 
        {
            // retry
        }

    }
}     


}

回答1:


Create a layout container and put the AdView and the renderView in it:

RelativeLayout layout = new RelativeLayout(this);
AdView adView = new AdView(this, AdSize.BANNER, "a151bf25136cf46");
layout.addView(renderView);
layout.addView(adView);
setContentView(layout);
adView.loadAd(new AdRequest());



回答2:


I tried it last night and got cool result, see my code in Oncreate() of your Activity:

 LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(LinearLayout.VERTICAL);
    layout.setGravity(Gravity.BOTTOM|Gravity.CENTER);

    // Create a banner ad
    mAdView = new AdView(this);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");



    // Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");



    /*For Test On Real Device*/
    AdRequest adRequest=  new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);



    LinearLayout linearLayout = new LinearLayout(this);
    linearLayout.setId(R.id.linear);
    linearLayout.setGravity(Gravity.BOTTOM | Gravity.CENTER);


    LinearLayout.LayoutParams layoutParams1 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0, 9f);
    layoutParams1.gravity = Gravity.TOP;
    view.setLayoutParams(layoutParams1);

    int heightPixels = AdSize.FULL_BANNER.getHeightInPixels(this);

    LinearLayout.LayoutParams layoutParams2 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, heightPixels, 0f);
    layoutParams2.gravity = Gravity.BOTTOM;
    mAdView.setLayoutParams(layoutParams2);

    layout.addView(view);
    layout.addView(mAdView);


    // Start loading the ad.
    setContentView(layout);

this will show you the ads at the bottom, GL ;)



来源:https://stackoverflow.com/questions/17159247/android-admob-without-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!