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:
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:
new OnInitializationCompleteListener() {
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>)
android:name="com.google.android.gms.ads.AD_MANAGER_APP"
<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>
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:
private RewardedAd rewardedAd;
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(this, "id_anuncio_recompensado",
adRequest, new RewardedAdLoadCallback() {
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
Log.d("TAG", loadAdError.toString());
public void onAdLoaded(@NonNull RewardedAd ad) {
Log.d("TAG", "Ad was loaded.");
rewardedAd.show(this, new OnUserEarnedRewardListener() {
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
//Realizar las acciones necesarias cuando ve el anuncio recompensado
rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
public void onAdClicked() {
// Called when a click is recorded for an ad.
Log.d("TAG", "Ad was clicked.");
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.");
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e("TAG", "Ad failed to show fullscreen content.");
public void onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d("TAG", "Ad recorded an impression.");
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.
Ingeniero en Informática, Investigador, me encanta crear cosas o arreglarlas y darles una nueva vida. Escritor y poeta. Más de 20 APPs publicadas y un libro en Amazon.
Post Views: 0
Relacionado