Interstitial con premio è un tipo di formato dell'annuncio incentivato che ti consente di offrire premi per gli annunci che vengono visualizzati automaticamente durante le naturali transizioni delle app. A differenza degli annunci con premio, gli utenti non devono attivare la visualizzazione di un annuncio interstitial con premio.
Prerequisiti
- SDK Google Mobile Ads 19.2.0 o versioni successive.
- Completa la Guida introduttiva.
Implementazione
Di seguito sono riportati i passaggi principali per integrare gli annunci interstitial con premio:
- Carica un annuncio
- Registrati per i callback eventi a schermo intero
- Gestire la richiamata
- Visualizza l'annuncio
Carica un annuncio
Il caricamento di un annuncio viene eseguito utilizzando il metodo load()
statico nella
classe RewardedInterstitialAd
. Il metodo di caricamento richiede un contesto, il tuo ID
unità pubblicitaria, un oggetto AdManagerAdRequest
e un
RewardedInterstitialAdLoadCallback
per ricevere una notifica quando il caricamento dell'annuncio va a buon fine o
non va a buon fine. L'oggetto RewardedInterstitialAd
caricato viene fornito come parametro
nel callback onRewardedInterstitialAdLoaded()
.
L'esempio seguente mostra come caricare un RewardedInterstitialAd
in MainActivity
.
Java
public class MainActivity extends AppCompatActivity {
private RewardedInterstitialAd rewardedInterstitialAd;
private String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Thread(
() -> {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this, initializationStatus -> {});
// Load an ad on the main thread.
runOnUiThread(
() -> {
loadAd();
});
})
.start();
}
public void loadAd() {
// Use the test ad unit ID to load an ad.
RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded_interstitial",
new AdManagerAdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
Log.d(TAG, "Ad was loaded.");
rewardedInterstitialAd = ad;
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
}
Kotlin
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAd
import com.google.android.gms.ads.rewardedinterstitial.RewardedInterstitialAdLoadCallback
class MainActivity : AppCompactActivity() {
private var rewardedInterstitialAd? = null
private final var TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val backgroundScope = CoroutineScope(Dispatchers.IO)
backgroundScope.launch {
// Initialize the Google Mobile Ads SDK on a background thread.
MobileAds.initialize(this@MainActivity) {}
// Load an ad on the main thread.
runOnUiThread {
loadAd()
}
}
}
private fun loadAd() {
RewardedInterstitialAd.load(this, "/21775744923/example/rewarded_interstitial",
AdManagerAdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
Log.d(TAG, "Ad was loaded.")
rewardedInterstitialAd = ad
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
}
Registrati per le richiamate
Per ricevere notifiche per gli eventi di presentazione, devi passare
un oggetto FullScreenContentCallback
al setter del tuo annuncio. L'oggetto
FullScreenContentCallback
gestisce i callback per quando l'annuncio viene presentato
correttamente o non va a buon fine e quando viene ignorato. Il seguente codice mostra come impostare un oggetto FullScreenContentCallback
anonimo all'interno di RewardedInterstitialAdLoadCallback
:
Java
public void loadAd(){
RewardedInterstitialAd.load(MainActivity.this, "/21775744923/example/rewarded_interstitial",
new AdManagerAdRequest.Builder().build(), new RewardedInterstitialAdLoadCallback() {
@Override
public void onAdLoaded(RewardedInterstitialAd ad) {
rewardedInterstitialAd = ad;
rewardedInterstitialAd.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.");
rewardedInterstitialAd = null;
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.");
rewardedInterstitialAd = 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.");
}
});
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
Log.d(TAG, loadAdError.toString());
rewardedInterstitialAd = null;
}
});
}
Kotlin
private fun loadAd() {
RewardedInterstitialAd.load(this, "/21775744923/example/rewarded_interstitial",
AdManagerAdRequest.Builder().build(), object : RewardedInterstitialAdLoadCallback() {
override fun onAdLoaded(ad: RewardedInterstitialAd) {
rewardedInterstitialAd = ad
rewardedInterstitialAd?.fullScreenContentCallback = object: FullScreenContentCallback() {
override fun onAdClicked() {
// Called when a click is recorded for an ad.
Log.d(TAG, "Ad was clicked.")
}
override fun 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.")
rewardedInterstitialAd = null
}
override fun onAdFailedToShowFullScreenContent(adError: AdError) {
// Called when ad fails to show.
Log.e(TAG, "Ad failed to show fullscreen content.")
rewardedInterstitialAd = null
}
override fun onAdImpression() {
// Called when an impression is recorded for an ad.
Log.d(TAG, "Ad recorded an impression.")
}
override fun onAdShowedFullScreenContent() {
// Called when ad is shown.
Log.d(TAG, "Ad showed fullscreen content.")
}
}
}
override fun onAdFailedToLoad(adError: LoadAdError) {
Log.d(TAG, adError?.toString())
rewardedInterstitialAd = null
}
})
}
Gestire i premi
Per mostrare il tuo annuncio interstitial con premio, implementa
l'interfaccia OnUserEarnedRewardListener
in MainActivity
, in modo da ricevere una notifica
quando l'utente ottiene un premio.
Java
public class MainActivity extends AppCompatActivity implements OnUserEarnedRewardListener {
...
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "User earned reward.");
// TODO: Reward the user!
}
}
Kotlin
class MainActivity : AppCompatActivity(), OnUserEarnedRewardListener {
...
override fun onUserEarnedReward(rewardItem: RewardItem) {
Log.d(TAG, "User earned reward.")
// TODO: Reward the user!
}
}
Mostra l'annuncio
Dopo aver implementato l'interfaccia OnUserEarnedRewardListener
, puoi presentare l'annuncio utilizzando il metodo show()
dell'annuncio, ad esempio:
Java
rewardedInterstitialAd.show(/* Activity */ MainActivity.this,/*
OnUserEarnedRewardListener */ MainActivity.this);
Kotlin
rewardedInterstitialAd?.show(/* Activity */ this, /*
OnUserEarnedRewardListener */ this)
Esempi su GitHub
Passaggi successivi
Esplora i seguenti argomenti: