插屏广告
加载插屏广告
要加载插屏广告,请首先创建一个对应您广告单元的 MaxInterstitialAd
对象。
然后调用该对象的 loadAd()
方法。
实现 MaxAdListener
以便在广告就绪时收到通知 (您也可以收到其他广告相关事件的通知)。
public class ExampleActivity extends Activity implements MaxAdListener{ private MaxInterstitialAd interstitialAd; private int retryAttempt;
void createInterstitialAd() { interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»", getApplicationContext() ); interstitialAd.setListener( this );
// Load the first ad interstitialAd.loadAd(); }
// MAX Ad Listener @Override public void onAdLoaded(final MaxAd maxAd) { // Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0; }
@Override public void onAdLoadFailed(final String adUnitId, final MaxError error) { // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++; long delayMillis = TimeUnit.SECONDS.toMillis( (long) Math.pow( 2, Math.min( 6, retryAttempt ) ) );
new Handler().postDelayed( new Runnable() { @Override public void run() { interstitialAd.loadAd(); } }, delayMillis ); }
@Override public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) { // Interstitial ad failed to display. AppLovin recommends that you load the next ad. interstitialAd.loadAd(); }
@Override public void onAdDisplayed(final MaxAd maxAd) {}
@Override public void onAdClicked(final MaxAd maxAd) {}
@Override public void onAdHidden(final MaxAd maxAd) { // Interstitial ad is hidden. Pre-load the next ad interstitialAd.loadAd(); }}
class ExampleActivity : Activity(), MaxAdListener{ private lateinit var interstitialAd: MaxInterstitialAd private var retryAttempt = 0.0
fun createInterstitialAd() { interstitialAd = MaxInterstitialAd( "«ad-unit-ID»", applicationContext ) interstitialAd.setListener( this )
// Load the first ad interstitialAd.loadAd() }
// MAX Ad Listener override fun onAdLoaded(maxAd: MaxAd) { // Interstitial ad is ready to be shown. interstitialAd.isReady() will now return 'true'
// Reset retry attempt retryAttempt = 0.0 }
override fun onAdLoadFailed(adUnitId: String?, error: MaxError?) { // Interstitial ad failed to load // AppLovin recommends that you retry with exponentially higher delays up to a maximum delay (in this case 64 seconds)
retryAttempt++ val delayMillis = TimeUnit.SECONDS.toMillis( Math.pow( 2.0, Math.min( 6.0, retryAttempt ) ).toLong() )
Handler().postDelayed( { interstitialAd.loadAd() }, delayMillis ) }
override fun onAdDisplayFailed(ad: MaxAd?, error: MaxError?) { // Interstitial ad failed to display. AppLovin recommends that you load the next ad. interstitialAd.loadAd() }
override fun onAdDisplayed(maxAd: MaxAd) {}
override fun onAdClicked(maxAd: MaxAd) {}
override fun onAdHidden(maxAd: MaxAd) { // Interstitial ad is hidden. Pre-load the next ad interstitialAd.loadAd() }}
展示插屏广告
要展示插屏广告,请在刚才创建的 MaxInterstitialAd
实例对象上调用 showAd( this )
。
if ( interstitialAd.isReady() ){ // `this` is the activity that will be used to show the ad interstitialAd.showAd( this );}
if ( interstitialAd.isReady ){ // `this` is the activity that will be used to show the ad interstitialAd.showAd(this)}
锁屏广告
AppLovin MAX SDK 提供了可在锁屏上展示插页广告的API。 此集成的用例包括音频应用程序,它们通常出现在锁定屏幕上。
加载锁屏广告
在锁屏上加载插屏广告的方式与通常加载插屏广告的方式类似。该过程如上所述。
但是您需要为 MaxInterstitialAd
设置一个额外参数,且传入的 Activity
必须实现 LifecycleOwner
界面。
⋮import androidx.lifecycle.LifecycleOwner;⋮
public class ExampleActivity extends Activity implements MaxAdListener, LifecycleOwner{ private FrameLayout adContainerView; private MaxInterstitialAd interstitialAd; private int retryAttempt;
void createInterstitialAd() { interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»", getApplicationContext() ); interstitialAd.setExtraParameter( "container_view_ads", "true" ); interstitialAd.setListener( this );
// Load the first ad interstitialAd.loadAd(); }
// MAX Ad Listener ⋮}
⋮import androidx.lifecycle.LifecycleOwner⋮
class ExampleActivity : Activity(), MaxAdListener, LifecycleOwner{ private lateinit var adContainerView: FrameLayout private lateinit var interstitialAd: MaxInterstitialAd private var retryAttempt = 0.0
fun createInterstitialAd() { interstitialAd = MaxInterstitialAd( "«ad-unit-ID»", applicationContext ) interstitialAd.setExtraParameter( "container_view_ads", "true" ) interstitialAd.setListener( this )
// Load the first ad interstitialAd.loadAd() }
// MAX Ad Listener ⋮}
显示锁屏广告
要显示锁屏广告,请在广告中使用 ViewGroup
来调用 showAd(…)
。
if ( interstitialAd.isReady() ){ // `this` is the activity that will be used to show the ad interstitialAd.showAd( adContainerView, getLifecycle(), this );}
if ( interstitialAd.isReady ){ // `this` is the activity that will be used to show the ad interstitialAd.showAd( adContainerView, getLifecycle(), this )}
中介网络支持
支持该功能的网络是AppLovin Bidding和AppLovin Exchange。
为适配器添加自定义支持
要为自定义适配器或我们的开源适配器添加支持,请重写下列 showInterstitialAd(…)
方法。
@Overridepublic void showInterstitialAd(final MaxAdapterResponseParameters parameters, final ViewGroup containerView, final Lifecycle lifecycle, final Activity activity, final MaxInterstitialAdapterListener listener){ ⋮}