跳转到内容

手动下载 MAX 的广告平台

要接收版本更新,请订阅 AppLovin-MAX-SDK-Android GitHub 库

下载最新 Android SDK

向项目添加 SDK

解压缩下载的文件,然后将 aar 文件拖拽至项目中的 libs 文件夹。 如果您的项目没有 libs 文件夹,请在 app 文件夹中创建。

Gradle

将下列行添加至您的 build.gradle 文件:

repositories {
google()
mavenCentral()
flatDir {
dirs 'libs'
}
}
dependencies {
implementation 'com.applovin:applovin-sdk:«x.y.z»@aar'
}

添加 SDK Key

将下列行添加至您的 AndroidManifest.xml 文件,并将它放置在 application 标签中:

<meta-data android:name="applovin.sdk.key" android:value="«your-SDK-key»"/>

您可以在 AppLovin 控制面板的 Account > General > Keys 部分找到 SDK Key。

启用 Ad Review

要启用 MAX Ad Review 服务,请将下列行添加至您的 build.gradle 文件:

针对根层级 build.gradle 文件的补充行

buildscript {
repositories {
maven { url 'https://artifacts.applovin.com/android' }
}
dependencies {
classpath "com.applovin.quality:AppLovinQualityServiceGradlePlugin:+"
}
}

针对应用层级 build.gradle 文件的补充行

apply plugin: 'applovin-quality-service'
applovin {
apiKey "«your-ad-review-key»"
}

您可以在 AppLovin 控制面板的 Account > General > Keys 部分找到 Ad Review Key。

初始化 SDK

调用 initializeSdk() 方法,初始化 SDK,向该方法传递一个 context 请在应用启动后尽快执行此操作,例如在启动 ActivityonCreate() 方法中。

public class MainActivity extends Activity
{
protected void onCreate(Bundle savedInstanceState)
{
// Set the mediation provider value to "max" to ensure proper functionality.
AppLovinSdk.getInstance( this ).setMediationProvider( "max" );
AppLovinSdk.initializeSdk( this, new AppLovinSdk.SdkInitializationListener() {
@Override
public void onSdkInitialized(final AppLovinSdkConfiguration configuration)
{
// AppLovin SDK is initialized, start loading ads
}
} );
}
}

插屏广告

加载插屏广告

要加载插屏广告,请使用您的广告单元创建一个 MaxInterstitialAd 对象,并调用 loadAd()。实现 MaxAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。

public class ExampleActivity extends Activity
implements MaxAdListener
{
private MaxInterstitialAd interstitialAd;
private int retryAttempt;
void createInterstitialAd()
{
interstitialAd = new MaxInterstitialAd( "«ad-unit-ID»", this );
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();
}
}

显示插屏广告

要展示插屏广告,请在刚才创建的 MaxInterstitialAd 实例对象上调用 showAd()

if ( interstitialAd.isReady() )
{
interstitialAd.showAd();
}

激励广告

加载激励广告

要加载激励广告,请使用您的激励广告单元调取一个 MaxRewardedAd 对象,并对其调用 loadAd()。 安装 MaxRewardedAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。

public class ExampleActivity extends Activity
implements MaxRewardedAdListener
{
private MaxRewardedAd rewardedAd;
private int retryAttempt;
void createRewardedAd()
{
rewardedAd = MaxRewardedAd.getInstance( "«ad-unit-ID»", this );
rewardedAd.setListener( this );
rewardedAd.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd)
{
// Rewarded ad is ready to be shown. rewardedAd.isReady() will now return 'true'
// Reset retry attempt
retryAttempt = 0;
}
@Override
public void onAdLoadFailed(final String adUnitId, final int errorCode)
{
// Rewarded 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()
{
rewardedAd.loadAd();
}
}, delayMillis );
}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error)
{
// Rewarded ad failed to display. AppLovin recommends that you load the next ad
rewardedAd.loadAd();
}
@Override
public void onAdDisplayed(final MaxAd maxAd) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdHidden(final MaxAd maxAd)
{
// rewarded ad is hidden. Pre-load the next ad
rewardedAd.loadAd();
}
@Override
public void onUserRewarded(final MaxAd maxAd, final MaxReward maxReward)
{
// Rewarded ad was displayed and user should receive the reward
}
}

显示激励广告

要展示激励广告,请在刚才创建的 MaxRewardedAd 实例对象上调用 showAd()

if ( rewardedAd.isReady() )
{
rewardedAd.showAd();
}

横幅和 MREC

通过程式化方法加载和展示横幅和 MREC

要加载横幅广告或 MREC,请使用您的广告单元创建一个 MaxAdView 对象,并调用 loadAd()。要展示广告,请将 MaxAdView 对象添加为视图层级的子视图。实现 MaxAdViewAdListener,以便在广告准备就绪以及发生其他广告相关事件时收到通知。

public class ExampleActivity extends Activity
implements MaxAdViewAdListener
{
private MaxAdView adView;
void createBannerAd()
{
adView = new MaxAdView( "«ad-unit-ID»", this );
adView.setListener( this );
// Stretch to the width of the screen for banners to be fully functional
int width = ViewGroup.LayoutParams.MATCH_PARENT;
// Banner height on phones and tablets is 50 and 90, respectively
int heightPx = getResources().getDimensionPixelSize( R.dimen.banner_height );
adView.setLayoutParams( new FrameLayout.LayoutParams( width, heightPx ) );
// Set background or background color for banners to be fully functional
adView.setBackgroundColor( ... );
ViewGroup rootView = findViewById( android.R.id.content );
rootView.addView( adView );
// Load the ad
adView.loadAd();
}
// MAX Ad Listener
@Override
public void onAdLoaded(final MaxAd maxAd) {}
@Override
public void onAdLoadFailed(final String adUnitId, final int errorCode) {}
@Override
public void onAdDisplayFailed(final MaxAd maxAd, final MaxError error) {}
@Override
public void onAdClicked(final MaxAd maxAd) {}
@Override
public void onAdExpanded(final MaxAd maxAd) {}
@Override
public void onAdCollapsed(final MaxAd maxAd) {}
@Override
public void onAdDisplayed(final MaxAd maxAd) { /* use this for impression tracking */ }
@Override
public void onAdHidden(final MaxAd maxAd) { /* DO NOT USE - THIS IS RESERVED FOR FULLSCREEN ADS ONLY AND WILL BE REMOVED IN A FUTURE SDK RELEASE */ }
}

在 Layout Editor 中加载和展示横幅或 MREC

您也可以将 MAX 横幅或 MREC 广告添加到视图布局 XML 中。 设置背景或背景色 (android:background),确保广告功能齐全。 如果是横幅,请将宽度 (android:layout_width) 拉伸至与屏幕相同的宽度。 对于 MREC,请相应地设置 android:adFormat

<com.applovin.mediation.ads.MaxAdView
xmlns:maxads="http://schemas.applovin.com/android/1.0"
maxads:adUnitId="«ad-unit-ID»"
android:background="@color/banner_background_color"
android:layout_width="match_parent"
android:layout_height="@dimen/banner_height" />

res/values/attrs.xml 中声明基础横幅高度为 50dp:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">50dp</dimen>
</resources>

res/values-sw600dp/attrs.xml 中声明平板电脑横幅高度为 90dp:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<dimen name="banner_height">90dp</dimen>
</resources>

要隐藏横幅或 MREC 广告,请进行如下调用:

adView.setVisibility( View.GONE );
adView.stopAutoRefresh();

要显示横幅或 MREC 广告,请进行以下调用:

adView.setVisibility( View.VISIBLE );
adView.startAutoRefresh();

选择要集成的广告平台

选择要集成的广告平台, 然后按照具体说明操作。

手动下载 MAX 的广告平台