0

Tengo una app, en la que quiero que al cambiar de activity, muestre un intersticial..El problema es que con el codigo que tengo (He seguido las recomendaciones de admob ), no me carga error, trabaja normal la app, pero no me muestra el anuncio de prueba. El anuncio smart banner lo carga perfecto, pero el intersticial no aparece. No estoy pudiendo ver donde esta el prblema, espero me guien al exito!! Dejo el codigo de la actividad principal para ver si alguno me da una mano, ya que soy bastante novato. Gracias!

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.gms.ads.AdActivity;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;

import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;

import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;


public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    private static String Ads_interstitial;
    //variable de interticial
    private InterstitialAd mInterstitialAd;



    private InterstitialAd interstitial;

    private String TAG = AdActivity.class.getSimpleName();



//variable de interticial


    // creating three buttons
    // by the of btn1, btn2,btn3
    Button btn1, btn2 ,btn3;



    //------------DOBLE BACK PRESSED PARA SALIR----------------------//
    private static final int TIME_DELAY = 2000;
    private static long back_pressed;
    //------------DOBLE BACK PRESSED PARA SALIR----------------------//


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);









                            //------------ADMOB BANNER----------------------//

                            //Call the function to initialize AdMob SDK
                            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                                @Override
                                public void onInitializationComplete(InitializationStatus initializationStatus) {

                                    // on below line displaying a log that admob ads has been initialized.
                                    Log.i("Admob", "Admob Initialized.");
                                }
                            });

                            // on below line creating and initializing variable for adView.
                            AdView adView = findViewById(R.id.adView);
                            // on below line creating and initializing variable for adRequest
                            AdRequest adRequest = new AdRequest.Builder().build();
                            // on below line loading request inside our adview.
                            adView.loadAd(adRequest);

//------------ADMOB BANNER----------------------//

                            // connecting buttons with the
                            // layout using findViewById()
                            btn1 = findViewById(R.id.button);
                            btn2 = findViewById(R.id.button2);
                            btn3 = findViewById(R.id.button3);

                            // apply setOnClickListener over buttons
                            btn1.setOnClickListener(this);
                            btn2.setOnClickListener(this);
                            btn3.setOnClickListener(this);


                        }


//------------DOBLE BACK PRESSED PARA SALIR----------------------//

                        @Override
                        public void onBackPressed() {
                            if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
                                super.onBackPressed();
                            } else {
                                Toast.makeText(getBaseContext(), "Presione otra vez para salir!",
                                        Toast.LENGTH_SHORT).show();
                            }
                            back_pressed = System.currentTimeMillis();
                        }

//------------DOBLE BACK PRESSED PARA SALIR----------------------//





                        // common onClick() for all buttons
                        @Override
                        public void onClick(View v) {

                            switch (v.getId()) {

                                // cases applied over different buttons
                                case R.id.button:

                                    // Toast message appears when button pressed
                                    Toast.makeText(this, "Cargando...", Toast.LENGTH_SHORT).show();
                                    //Iniciando la actividad Visor
                                    Intent intent = new Intent(this, agile.class);
                                    startActivity(intent);

                                    break;

                                case R.id.button2:

                                    Toast.makeText(this, "Cargando...", Toast.LENGTH_SHORT).show();
                                    //Iniciando la actividad Visor
                                    intent = new Intent(this, mail.class);
                                    startActivity(intent);
                                    break;


                            }


// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER

                            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                                @Override
                                public void onInitializationComplete(InitializationStatus initializationStatus) {}
                            });
                            AdRequest adRequest = new AdRequest.Builder().build();

                            InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
                                    new InterstitialAdLoadCallback() {
                                        @Override
                                        public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                            // The mInterstitialAd reference will be null until
                                            // an ad is loaded.
                                            mInterstitialAd = interstitialAd;
                                            Log.i(TAG, "onAdLoaded");
                                        }

                                        @Override
                                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                            // Handle the error
                                            Log.d(TAG, loadAdError.toString());
                                            mInterstitialAd = null;






                                            mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
                                                @Override
                                                public void onAdClicked() {
                                                    // Called when a click is recorded for an ad.
                                                    Log.d(TAG, "Ad was clicked.");
                                                }

                                                @Override
                                                public void onAdDismissedFullScreenContent() {
                                                    // Called when ad is dismissed.
                                                    // Set the ad reference to null so you don't show the ad a second time.
                                                    Log.d(TAG, "Ad dismissed fullscreen content.");
                                                    mInterstitialAd = null;
                                                }

                                                @Override
                                                public void onAdFailedToShowFullScreenContent(AdError adError) {
                                                    // Called when ad fails to show.
                                                    Log.e(TAG, "Ad failed to show fullscreen content.");
                                                    mInterstitialAd = null;
                                                }

                                                @Override
                                                public void onAdImpression() {
                                                    // Called when an impression is recorded for an ad.
                                                    Log.d(TAG, "Ad recorded an impression.");
                                                }

                                                @Override
                                                public void onAdShowedFullScreenContent() {
                                                    // Called when ad is shown.
                                                    Log.d(TAG, "Ad showed fullscreen content.");




                                                    if (mInterstitialAd != null) {
                                                        mInterstitialAd.show(MainActivity.this);
                                                    } else {
                                                        Log.d("TAG", "The interstitial ad wasn't ready yet.");

                                                    }
// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER// ADS INTER//
                                                }
                                            })
                                        ;}
                                    })
                            ;}
                        }
1
  • Es importante agregues el error que obtienes en el LogCat. Esto hace más fácil determinar el problema.
    – Jorgesys
    Commented el 24 mar. 2023 a las 5:03

1 respuesta 1

0

El error principal es que no estas cargando el anuncio correctamente, el anuncio se debe cargar al llamarse la función onAdLoaded(),

  ...
  ...
  InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
                                new InterstitialAdLoadCallback() {
                                    @Override
                                    public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
                                        // The mInterstitialAd reference will be null until
                                        // an ad is loaded.
                                        mInterstitialAd = interstitialAd;
                                        Log.i(TAG, "onAdLoaded");

                                                //* Carga anuncio.
                                                if (mInterstitialAd != null) {
                                                    mInterstitialAd.show(MainActivity.this);
                                                } else {
                                                    Log.d("TAG", "The interstitial ad wasn't ready yet.");    
                                                }


                                    }
                                    ...
                                    ...

En onAdFailedToLoad() unicamente debes hacer la instancia del anuncio a valor null:

                                @Override
                                public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                                    // Handle the error
                                    Log.e(TAG, loadAdError.toString());
                                    mInterstitialAd = null;
                                 }
                           

jorgesys admob

2
  • gracias, era eso...pero lo que veo es que abre la actividad siguiente, y recien desps que pasa 1 segundo aprox, carga el intesticial, Revise el codigo y no veo porque tarda en cargar, habra forma de que cargue antes la publicidad que el activity?
    – Gaby
    Commented el 25 mar. 2023 a las 15:14
  • Que bien que ahora carga tu anuncio, desafortunadamente no hay forma de que cargue automáticamente, la carga es asíncrona así que regularmente tarda unos segundos.
    – Jorgesys
    Commented el 25 mar. 2023 a las 17:44

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.