如何在eclipse中将我的intertitial广告添加到此代码中?

时间:2015-12-26 00:30:04

标签: admob

大家好,我想有人帮我添加intertitial admob代码到这个游戏,只有横幅代码,但我想要的是在屏幕上一直显示横幅,而不仅仅是在显示消息时。这是代码:

  • Mainfile.java:
package com.boontaran.games.chickenrun;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;

import com.badlogic.gdx.Application;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.backends.android.AndroidApplication;

import com.google.analytics.tracking.android.Fields;
import com.google.analytics.tracking.android.GoogleAnalytics;
import com.google.analytics.tracking.android.MapBuilder;
import com.google.analytics.tracking.android.Tracker;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class ChickenRunAndroid extends AndroidApplication implements Ihandler {
    private ChickenRun game;

    private RelativeLayout layout;


    //IMPORTANT, change this...
    //admob
    private final String admobId = "###"; // change with your ads id, if not using ads, leave it blank --> ""
    private RelativeLayout.LayoutParams adscontainerParams ;
    //analytics
    private final String trackingId = "UA-69568893-33";// change with your tracking  id, if not using it, leave it blank --> ""


    //OPTIONAL
    // share message
    private final String shareTitle = "Chicken Run";
    private final String shareBody = "Hi, check out this grat game : ";

    //ads
    private AdView adView;
    private RelativeLayout adsContainer;

    //analytics
    private Tracker tracker;

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

        //fullscreen
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);


        //main app layout
        layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT);
        setContentView(layout , params);

        //the game
        game = new ChickenRun(this);
        View gameView = initializeForView(game);
        Gdx.app.setLogLevel(Application.LOG_NONE);

        //add game to view
        layout.addView(gameView);

        setupAdmob();
        setupTracker();
    }
    private void setupTracker() {
        if(trackingId.equals("")) return;

        GoogleAnalytics analytics = GoogleAnalytics.getInstance(this);
        // debug --> analytics.getLogger().setLogLevel(LogLevel.VERBOSE);

        tracker = analytics.getTracker(trackingId);


    }
    @Override
    public void sendMessage(int message) {
        if(message==ChickenRun.SHARE) {
            shareGame();
        }

        else if(message==ChickenRun.HIDE_BANNER) {
            handler.sendEmptyMessage(ChickenRun.HIDE_BANNER);







        }
        else if(message==ChickenRun.SHOW_BANNER) {
            handler.sendEmptyMessage(ChickenRun.SHOW_BANNER);
        }
    }

    @Override
    public void trackEvent(String name) {
        if(tracker ==null) return;

        tracker.send(MapBuilder.createEvent("Game", "action", name, (long) 1 ).build());
        Gdx.app.log("PenaltyChallengeAndroid", "event="+name);
    }
    @Override
    public void trackScreen(String name) {
        if(tracker==null) return;

        tracker.send(MapBuilder.createAppView().set(Fields.SCREEN_NAME, name).build());
        Gdx.app.log("ChickenRunAndroid", "track screen="+name);

    }

    private Handler handler = new Handler(){

        @Override
        public void handleMessage(Message msg) {
            if(msg.what ==ChickenRun.SHOW_BANNER) {
                if(adsContainer == null) return;
                showBanner();

            }
            else if(msg.what ==ChickenRun.HIDE_BANNER) {
                if(adsContainer == null) return;
                hideBanner();

            }
        }

    };

    private void showBanner() {
        layout.addView(adsContainer,adscontainerParams);
    }
    private void hideBanner() {
        layout.removeView(adsContainer);
    }
    private void shareGame() {
        Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
        sharingIntent.setType("text/plain");

        //set the data
        String packageName = getApplicationContext().getPackageName();

        String body = shareBody+"https://play.google.com/store/apps/details?id="+packageName;
        sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, shareTitle);
        sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, body);

        Gdx.app.log("ChickenRun", body);

        //start the share activity
        startActivity(Intent.createChooser(sharingIntent, "Share via"));
    }

    private void setupAdmob() {
        //not using admob
        if(admobId.equals("")) return;

        //container
        adscontainerParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.MATCH_PARENT, 
                RelativeLayout.LayoutParams.WRAP_CONTENT);

        adsContainer = new RelativeLayout(ChickenRunAndroid.this);




         // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(admobId);

        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .build();

        //.addTestDevice("635E0169A7DF83F621390AF96B6F06F7")

        // Start loading the ad in the background.
        adView.loadAd(adRequest);

        //final RelativeLayout container = (RelativeLayout) findViewById(R.id.bannerContainer2);

        Gdx.app.log("ChickenRunAndroid", "load ads");
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                Gdx.app.log("ChickenRunAndroid", "ad loaded");
                if(adView.getParent() == null) {
                    Gdx.app.log("ChickenRunAndroid", "ad showed");



                    RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.MATCH_PARENT, 
                            RelativeLayout.LayoutParams.WRAP_CONTENT);


                    adsContainer = new RelativeLayout(ChickenRunAndroid.this);


                    lp = new RelativeLayout.LayoutParams(
                            RelativeLayout.LayoutParams.WRAP_CONTENT, 
                            RelativeLayout.LayoutParams.WRAP_CONTENT);

                    lp.addRule(RelativeLayout.CENTER_HORIZONTAL);
                    adsContainer.addView(adView,lp);


                }
                super.onAdLoaded();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Gdx.app.log("ChickenRunAndroid", "ad failed");
                super.onAdFailedToLoad(errorCode);

            }

        });
    }

}

由于

1 个答案:

答案 0 :(得分:0)

这是documentation for integrating interstitial ads

浏览并在您的活动中插入所需的代码。不要指望有人为你做这件事,这样你就可以复制并开始使用了。尝试一下,如果你遇到问题,请告诉我,我会尽力帮助。