Customize Consent Preferences

We use cookies to help you navigate efficiently and perform certain functions. You will find detailed information about all cookies under each consent category below.

The cookies that are categorized as "Necessary" are stored on your browser as they are essential for enabling the basic functionalities of the site. ... 

Always Active

Necessary cookies are required to enable the basic features of this site, such as providing secure log-in or adjusting your consent preferences. These cookies do not store any personally identifiable data.

No cookies to display.

Functional cookies help perform certain functionalities like sharing the content of the website on social media platforms, collecting feedback, and other third-party features.

No cookies to display.

Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics such as the number of visitors, bounce rate, traffic source, etc.

No cookies to display.

Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.

No cookies to display.

Advertisement cookies are used to provide visitors with customized advertisements based on the pages you visited previously and to analyze the effectiveness of the ad campaigns.

No cookies to display.

Crear anuncio Admob rewarded recompensado en Android

Tiempo de lectura: 2 minutos

Hoy vamos a aprender cómo podemos añadir un anuncio Admob recompensado en Android con Java.

Primero implementaremos en nuestro build.gradle:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
implementation 'com.google.android.gms:play-services-ads:23.0.0'
implementation 'com.google.android.gms:play-services-ads:23.0.0'
    implementation 'com.google.android.gms:play-services-ads:23.0.0'

Después tenemos que inicializar los anuncios en nuestro MainActivity:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
MobileAds.initialize(
this,
new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
MobileAds.initialize( this, new OnInitializationCompleteListener() { @Override public void onInitializationComplete(InitializationStatus initializationStatus) {} });
    MobileAds.initialize(
        this,
        new OnInitializationCompleteListener() {
          @Override
          public void onInitializationComplete(InitializationStatus initializationStatus) {}
        });

Y añadimos en el manifest (debajo de </application>)

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<meta-data
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
android:value="true" />
<meta-data android:name="com.google.android.gms.ads.AD_MANAGER_APP" android:value="true" />
    <meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true" />

Y también dentro de <application> </application>

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="TU_ID_APLICACION_ADMOB" />
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" android:value="TU_ID_APLICACION_ADMOB" />
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="TU_ID_APLICACION_ADMOB" />

Cambia TU_ID_APLICACION_ADMOB por el id de tu APP de Admob.

Ahora vamos a crear nuestro anuncio recompensado:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
private RewardedAd rewardedAd;
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "id_anuncio_recompensado",
adRequest, new RewardedAdLoadCallback() {
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d("TAG", loadAdError.toString());
rewardedAd = null;
}
@Override
public void onAdLoaded(@NonNull RewardedAd ad) {
rewardedAd = ad;
Log.d("TAG", "Ad was loaded.");
rewardedAd.show(this, new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
//Realizar las acciones necesarias cuando ve el anuncio recompensado
}
});
rewardedAd.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.");
rewardedAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e("TAG", "Ad failed to show fullscreen content.");
rewardedAd = 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.
loader.setVisibility(View.GONE);
Log.d("TAG", "Ad showed fullscreen content.");
}
});
}
});
private RewardedAd rewardedAd; AdRequest adRequest = new AdRequest.Builder().build(); RewardedAd.load(this, "id_anuncio_recompensado", adRequest, new RewardedAdLoadCallback() { @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Handle the error. Log.d("TAG", loadAdError.toString()); rewardedAd = null; } @Override public void onAdLoaded(@NonNull RewardedAd ad) { rewardedAd = ad; Log.d("TAG", "Ad was loaded."); rewardedAd.show(this, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { //Realizar las acciones necesarias cuando ve el anuncio recompensado } }); rewardedAd.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."); rewardedAd = null; } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { // Called when ad fails to show. Log.e("TAG", "Ad failed to show fullscreen content."); rewardedAd = 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. loader.setVisibility(View.GONE); Log.d("TAG", "Ad showed fullscreen content."); } }); } });
    private RewardedAd rewardedAd;    

AdRequest adRequest = new AdRequest.Builder().build();

            RewardedAd.load(this, "id_anuncio_recompensado",
                    adRequest, new RewardedAdLoadCallback() {
                        @Override
                        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
                            // Handle the error.
                            Log.d("TAG", loadAdError.toString());
                            rewardedAd = null;
                        }

                        @Override
                        public void onAdLoaded(@NonNull RewardedAd ad) {
                            rewardedAd = ad;
                            Log.d("TAG", "Ad was loaded.");
                            rewardedAd.show(this, new OnUserEarnedRewardListener() {
                                @Override
                                public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
                                  //Realizar las acciones necesarias cuando ve el anuncio recompensado
                                }
                            });

  rewardedAd.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.");
                    rewardedAd = null;
                }

                @Override
                public void onAdFailedToShowFullScreenContent(AdError adError) {
                    // Called when ad fails to show.
                    Log.e("TAG", "Ad failed to show fullscreen content.");
                    rewardedAd = 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.
                    loader.setVisibility(View.GONE);
                    Log.d("TAG", "Ad showed fullscreen content.");
                }
            });

                        }
                    });

          

Aqui: «id_anuncio_recompensado» indicaremos nuestro id de anuncio recompensado.

Y aquí podremos realizar la acción necesaria con el anuncio recompensado una vez visualizado: //Realizar las acciones necesarias cuando ve el anuncio recompensado

Con esto mostraremos el anuncio recompensado.

0

Deja un comentario