简体   繁体   English

AdMob非页内广告不能点击

[英]AdMob Interstitial ads not clickable

I successfully implemented AdMob interstitial ads into my application but the only problem is that they are not clickable. 我已将AdMob插页式广告成功实现到我的应用程序中,但唯一的问题是它们不可点击。

Here is my AdMob.java class: 这是我的AdMob.java类:

import android.app.Activity;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class Admob extends AdListener{

    private Activity ctx;

    //Admob
    private InterstitialAd interstitial;
    private AdRequest adRequest;

    private String unitId;

    public static boolean fromAdmob = false;

    public Admob(Activity ctx, String unitId)
    {
        this.ctx    = ctx;
        this.unitId = unitId;

        try{
            this.interstitial = new InterstitialAd(this.ctx);
            interstitial.setAdUnitId(unitId);

            interstitial.setAdListener(this);
            adRequest = new AdRequest.Builder().build();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    public void loadAd()
    {
        try{
            interstitial.loadAd(adRequest);
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onAdLoaded() {
        try{
            fromAdmob = true;
            interstitial.show();
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }

    @Override
    public void onAdFailedToLoad(int errorCode)
    {

    }

    @Override
    public void onAdClosed() {
        fromAdmob = false;
    }

}

And the MainPageActivity.java file: 和MainPageActivity.java文件:

public class MainPageActivity extends Activity implements IGameListener, IEventDelegate{

    //Tags
    private static final String TAG = "bubblesMainPage";

    //Context
    public static Activity instance;

    //Media
    private MediaPlayer mp;

    //Ads
    private Admob admob;

    //TapSDK
    private Tap tap;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_page_activity);

        overridePendingTransition(R.anim.fade_in, R.anim.fade_out);

        tr
            Tap.establish(this);
            tap = new Tap(this, "xxxx");
        }
        catch (Exception e){
            e.printStackTrace();
        }


        //Admob
        this.admob = new Admob(this, "ADMOB_ID");

        //Context
        instance = this;

        //Media
        mp = MediaPlayer.create(getApplicationContext(), R.raw.background_music);
        mp.setLooping(true);


      //Set activity properties
        try {
            requestWindowFeature(Window.FEATURE_NO_TITLE);

            getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        } catch (Exception e) {
            e.printStackTrace();
        }

        //Intents & Listeners
        final Intent gamePlayActivity = new Intent(this, BubbleShooterActivity.class);
        BubbleShooterActivity.listener = this;


        //Animations
        Animation scaleUpAnim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.scale_up);
        findViewById(R.id.play_button).startAnimation(scaleUpAnim);

        //UI Events
        findViewById(R.id.play_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            startActivity(gamePlayActivity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }).start();


            }
        });

        findViewById(R.id.more_games_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                tap.showMoreGamesWithViewControllerAndAppID((IEventDelegate)instance);

            }
        });

    }

    /*
    Game methods - start
     */

    @Override
    public void onGameStart() {
        Log.i(TAG, "onGameStart");
    }

    @Override
    public void onLevelFailed() {

        //Level failed window
        try{
            Intent i = new Intent(this, LostActivity.class);
            startActivity(i);
        }
        catch (Exception e){
            e.printStackTrace();
        }

        Log.i(TAG, "onLevelFailed");
    }

    @Override
    public void onLevelUp() {
        Log.i(TAG, "onLevelUp");

        if(admob != null){
            runOnUiThread(new Runnable() {
                @Override
                public void run() {

                    try{
                        admob.loadAd();
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }

                    try{
                        Intent i = new Intent(instance, WonActivity.class);
                        startActivity(i);
                    }
                    catch (Exception e){
                        e.printStackTrace();
                    }
                }
            });
        }

    }

    @Override
    public void onGameEnd() {
        Log.i(TAG, "onGameEnd");
    }

    /*
    TapSDK methods - start
     */
    @Override
    public void viewDidAppear() {
        Log.i(TAG, "TapSDK: viewDidAppear");
    }

    @Override
    public void viewDidDisappear() {
        Log.i(TAG, "TapSDK: viewDidDisappear");
    }

    @Override
    public void viewConnectFail(String s) {
        Log.i(TAG, "TapSDK: viewConnectFail " + s);
    }

    @Override
    public void conversion() {
        Log.i(TAG, "TapSDK: conversion");
    }

    @Override
    public void onOfferComplete(int score) {
        Log.i(TAG, "TapSDK: onOfferComplete " + score);
    }

    /*
    Activity methods start
     */

    @Override
    public void onBackPressed() {

        Log.i(TAG, "onBackPressed");

        try{
            Intent exitScreen = new Intent(this, ExitActivity.class);
            startActivity(exitScreen);
        }
        catch (Exception e){
            e.printStackTrace();
            Log.e(TAG, "onBackPressed: " + e.getMessage());
        }

    }

    @Override
    protected void onStart() {
        super.onStart();

        // Monitor launch times and interval from installation
        RateThisApp.onStart(this);
        // If the criteria is satisfied, "Rate this app" dialog will be shown
        RateThisApp.showRateDialogIfNeeded(this);
    }

    @Override
    protected void onResume()
    {
        super.onResume();

        if(tap != null)
            tap.onResume();

        mp.seekTo(0);
        mp.start();
    }

    @Override
    protected void onPause()
    {
        if(tap != null)
            tap.onPause();

        mp.pause();

        super.onPause();
    }

    @Override
    protected void onDestroy()
    {
        mp.stop();
        mp = null;

        if(tap != null)
            tap.onDestroy();

        super.onDestroy();
    }


}

I couldn't figure out how to show the interstitial ad when the user levels up and presses the play button (which will lead him to the next level). 当用户升级并按下播放按钮时,我不知道如何显示插页式广告(这将使他进入下一个级别)。 The interstitial ad show up when he presses the home button which will take him to the MainActivity screen. 当他按下主屏幕按钮时,插页式广告就会显示出来,这将带他进入MainActivity屏幕。

Can someone help me to make the ads clickable? 有人可以帮助我使广告可点击吗? Also, any suggestions on how to add the interstitial ad after each completed level? 另外,关于在每个完成的关卡之后如何添加插页式广告的建议? When someone has finished a level and presses the play button, an interstitial ad should appear and when that is closed he should be able to play the next level. 当某人完成一个关卡并按下“播放”按钮时,将会出现一个插页式广告,当该广告关闭时,他应该能够播放下一个关卡。

I really appreciate your help! 非常感谢您的帮助!

A number of issues: 若干问题:

  1. Don't call interstitial.show() from onAdLoaded . 不要从onAdLoaded调用interstitial.show() Call show() when the user presses the play button for your next level (but only if an ad is available). 当用户按下下一级的播放按钮时调用show() (但仅当有广告可用时)。
  2. Get rid of public static Activity instance; 摆脱public static Activity instance; Never hold a static reference to an Activity. 永远不要持有对活动的静态引用。 You will just leak context everywhere. 您只会在各处泄漏上下文。
  3. Instead of calling admob.loadAd() from onLevelUp() call it as soon as you create your Activity (it takes times to load an ad from the network). 创建活动后,您无需admob.loadAd()onLevelUp()调用onLevelUp()调用(从网络加载广告需要花费时间)。 NB you don't need to call it from runOnUIThread() 注意,您不需要从runOnUIThread()调用它
  4. Call admob.loadAd() again after each time you show the ad to the user. 每次向用户显示广告后,再次调用admob.loadAd()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM