跳转到内容

原生广告

Native ads let you monetize your app in a way that’s consistent with its existing design. The AppLovin MAX SDK gives you access to an ad’s individual assets. This way, you can design the ad layout to be consistent with the look and feel of your app. The SDK automatically caches images and tracks metrics. You can focus on how, when, and where to display ads.

手动

如果您有自定义视图并且想要手动将原生广告资源加载到这些视图中,请使用此 API。 这种集成方法主要涉及三个步骤:

  1. 绑定 UI 组件。
  2. 加载原生广告。
  3. 销毁原生广告。

要使用手动 API,请在 Create New Ad Unit 界面中选择 Manual

Template: ☐ Small, ☐ Medium, ☒ Manual

绑定 UI 组件

You can bind custom UI components to the MAX SDK. Then you can render native ad assets into those components. The example below demonstrates this with custom views that you create with the Layout Editor and unique view IDs. You can also use this method if you create your views programmatically.

To accord with AppLovin’s policy, your ad must contain the Privacy Information icon. This icon links to an important privacy notice. You can bind to it via MaxNativeAdViewBinder.Builder#setOptionsContentViewGroupId(…).

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/icon_image_view"
… />
<LinearLayout
android:id="@+id/ad_options_view"
… />
<TextView
android:id="@+id/title_text_view"
… />
<FrameLayout
android:id="@+id/star_rating_view"
… />
<TextView
android:id="@+id/advertiser_textView"
… />
<TextView
android:id="@+id/body_text_view"
… />
<FrameLayout
android:id="@+id/media_view_container"
… />
<Button
android:id="@+id/cta_button"
… />
</androidx.constraintlayout.widget.ConstraintLayout>

下一步,使用实例为 MaxNativeAdViewBinder 的唯一标签 ID 绑定子视图。AppLovin 并不保证某个广告平台会返回特定资源。

public class ExampleActivity
extends Activity
implements MaxAdRevenueListener
{
private MaxNativeAdView createNativeAdView()
{
MaxNativeAdViewBinder binder = new MaxNativeAdViewBinder.Builder( R.layout.native_custom_ad_view )
.setTitleTextViewId( R.id.title_text_view )
.setBodyTextViewId( R.id.body_text_view )
.setStarRatingContentViewGroupId( R.id.star_rating_view )
.setAdvertiserTextViewId( R.id.advertiser_textView )
.setIconImageViewId( R.id.icon_image_view )
.setMediaContentViewGroupId( R.id.media_view_container )
.setOptionsContentViewGroupId( R.id.options_view )
.setCallToActionButtonId( R.id.cta_button )
.build();
return new MaxNativeAdView( binder, this );
}
}

加载原生广告

加载预渲染广告

要加载预渲染原生广告,请首先创建一个对应您广告单元 ID 的 MaxNativeAdLoader 对象,并调用 loadAd(MaxNativeAdView) 方法。 安装并设置 MaxNativeAdLoadListener,以便在原生广告的加载状态发生变化时获得通知。

广告加载与渲染分离

要加载原生广告,请首先创建一个对应您广告单元 ID 的 MaxNativeAdLoader 对象,并调用 loadAd() 方法。 安装并设置 MaxNativeAdLoadListener,以便在原生广告的加载状态发生变化时获得通知。然后使用 onNativeAdLoaded 中返回的 MaxAd 来呈现广告视图。您可以通过调用 MaxNativeAdLoader.render(MaxNativeAdView, MaxAd) 来实现。

销毁原生广告

If you stop using a native ad, destroy its resources by calling the destroy() method. If you do not, the performance of your app degrades over time. Below is an example of how you load and destroy a native ad. This takes place after you have bound the UI components from the previous step.

public class ExampleActivity
extends Activity
implements MaxAdRevenueListener
{
private ViewGroup nativeAdContainerView;
private MaxNativeAdLoader nativeAdLoader;
private MaxAd loadedNativeAd;
private void createNativeAdLoader()
{
nativeAdLoader = new MaxNativeAdLoader( "«ad-unit-ID»" );
nativeAdLoader.setRevenueListener( this );
nativeAdLoader.setNativeAdListener( new NativeAdListener() );
}
private void loadNativeAd()
{
nativeAdLoader.loadAd( createNativeAdView() );
}
@Override
public void onAdRevenuePaid(final MaxAd ad) { }
private class NativeAdListener
extends MaxNativeAdListener
{
@Override
public void onNativeAdLoaded(final MaxNativeAdView nativeAdView, final MaxAd nativeAd)
{
// Destroy any pre-existing native ad to prevent memory leaks.
if ( loadedNativeAd != null )
{
nativeAdLoader.destroy( loadedNativeAd );
}
// Save ad for cleanup.
loadedNativeAd = nativeAd;
nativeAdContainerView.removeAllViews();
nativeAdContainerView.addView( nativeAdView );
}
@Override
public void onNativeAdLoadFailed(final String adUnitId, final MaxError error)
{
// Native ad load failed.
// AppLovin recommends retrying with exponentially higher delays up to a maximum delay.
}
@Override
public void onNativeAdClicked(final MaxAd nativeAd) { }
}
@Override
protected void onDestroy()
{
// Destroy the native ad and native ad loader to prevent memory leaks.
if ( loadedNativeAd != null )
{
nativeAdLoader.destroy( loadedNativeAd );
}
nativeAdLoader.destroy();
super.onDestroy();
}
}

资源规模

AppLovin recommends that you incorporate as many of the native elements as are appropriate in the context of what the rest of your app looks like. These may include the Title and Media View or Icon. If you give the user more information, this helps them decide whether they want to click the ad.

对于 AppLovin Exchange 广告需求,标题、描述和 CTA 所允许的最大字符数如下:

资源最大字符数
标题50 个字符
描述150 个字符。可以在第 149 个字符后添加省略号 (...)作为第 150 个字符。
CTA15 个字符

对于 SDK 聚合的广告平台,最大字符数由广告平台设置。

如何获取媒体内容长宽比

下面的代码片段演示了如何获取您原生广告的媒体内容长宽比:

@Override
public void onNativeAdLoaded(final MaxNativeAdView adView, final MaxAd ad)
{
MaxNativeAd nativeAd = ad.getNativeAd();
if ( nativeAd != null )
{
float aspectRatio = nativeAd.getMediaContentAspectRatio();
}
}

星级评分

您可以访问并渲染所推广应用的星级评分。 在可用情况下,该值是 [0.0, 5.0] 范围内的浮点数。

MAX SDK 会自动在您指定为星级评定容器的容器视图中渲染星星。 如果网络不提供星级评定,或者星级评定 < 3,则 SDK 不会填充星级评定容器视图。 您有责任相应地调整您的布局。

要检索当前广告的星级:

@Override
public void onNativeAdLoaded(final MaxNativeAdView adView, final MaxAd ad)
{
MaxNativeAd nativeAd = ad.getNativeAd();
if ( nativeAd != null )
{
Double starRating = nativeAd.getStarRating();
if ( starRating == null || starRating < 3 )
{
// Star rating not available, or < 3 - hide star rating container view
return;
}
// Star rating available and >= 3 - show star rating container view
}
}

Ad Placer

广告投放器会使用 RecyclerView 自动将原生广告插入您现有的内容流中。 要集成此 API,请进行以下高级步骤:

  1. Configure native ad layout.
  2. 创建 Ad Placer 设置。
  3. Configure ad placer.
  4. 配置广告呈现设置。
  5. 可选地,设置监听器。

This page explains these steps in greater detail below.

To accord with AppLovin’s policy, your ad must contain the Privacy Information icon. This icon links to an important privacy notice. You can bind to it via MaxNativeAdViewBinder.Builder#setOptionsContentViewGroupId(…).

1. Configure Native Ad Layout

Ad placer supports manual native ad layouts. See the “Manual” section of this page to learn how to configure such ad layouts. See Configure Ad Rendering Settings below to learn how to configure the ad placer to support your layout.

2. 创建 Ad Placer 设置

使用您的广告单元标识符,创建一个 MaxAdPlacerSettings 对象。该对象用于配置您的 Ad Placer,并提供广告在信息流中的定位信息。

MaxAdPlacerSettings settings = new MaxAdPlacerSettings( "«ad-unit-ID»" );

配置广告位置

Ad Placer 将广告放置在您的信息流中。 它至少基于下列之一来实现这一点:

  1. 指示广告投放位置的固定索引路径列表
  2. a repeating interval at which to insert ads
    • Ad Placer 会在最后一个固定位置后插入重复广告。 如果您未设置任何固定位置,则 Ad Placer 会在流中的第一个位置后重复添加广告。 该间隔值必须大于或等于2。 小于 2 的间隔值会禁用 Ad Placer。

通过修改 MaxAdPlacerSettings 来配置广告位置

  1. 要添加新的固定广告位置(例如,在第 1 行):

    settings.addFixedPosition( 3 );


  2. 删除现有的固定位置:

    settings.resetFixedPositions();


  3. 要更改重复间隔(例如,在 feed 中每五个位置插入一个广告):

    settings.setRepeatingInterval( 5 );

其他设置

您可以通过 MaxAdPlacerSettings 调整这些设置来配置您的广告投放器:

maxAdCount
将其设置为流中的最大广告数量(默认值 = 256)。 如果流包含多个部分,则这将决定每个广告部分的最大广告数量。
maxPreloadedAdCount
将其设置为在流中预加载的最大广告数(默认值为 4)。

3. Configure Ad Placer

Choose one of these two options when you configure your ad placer:

  1. 如果您的内容流基于 RecyclerView,请使用 MaxRecyclerAdapter。 该辅助类对原始适配器进行了封装,能够自动渲染并将广告插入到内容流中。 您可以通过调用 getAdPlacer() 来访问底层广告投放器。
  2. 您可能更愿意创建子类或直接使用 MaxAdPlacer,例如当您的内容流使用了其他自定义 UI 组件时。

使用 MaxRecyclerAdapter

To configure your ad placer with MaxRecyclerAdapter follow these instructions:

  1. 使用您的设置、原始适配器和活动来对 MaxRecyclerAdapter 进行初始化 :

    adAdapter = new MaxRecyclerAdapter( settings, adapter, this );

  2. 将您的 Recycler View (回收视图) 的适配器设置为已创建的 MaxRecyclerAdapter:

    recyclerView.setAdapter( adAdapter );

  3. 更新您的代码以执行以下操作:
    • 无论何时您在流中添加或删除项目,请从适配器调用适当的方法。 这些方法会将您信息流的更新通知广告投放者:
      • notifyItemInserted()
      • notifyItemRemoved()
      • notifyItemsInserted()
      • notifyItemsRemoved()
    • 如果您在原始适配器上注册了任何数据观察者,请改为在 MaxRecyclerAdapter 上注册。 这样他们就能在广告插入后收到调整后的内容项位置。
    • 如果代码的任何其他部分依赖于广告插入前内容项的位置,请将位置转换为原始位置。 您可以通过调用 adAdapter.getOriginalPosition() 来实现。
  4. 调用 loadAds() 来启动加载广告:

    adAdapter.loadAds();

以下代码展示了如何使用 MaxRecyclerAdapter 将广告加载到 Recycler View 中:

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate( savedInstanceState );
// Create your own recycler view and original adapter
// Configure ad adapter
MaxAdPlacerSettings settings = new MaxAdPlacerSettings( "«ad-unit-ID»" );
settings.setRepeatingInterval( 5 );
adAdapter = new MaxRecyclerAdapter( settings, originalAdapter, this );
// Optionally, register listener
adAdapter.setListener( this );
// Configure recycler view and load ads
recyclerView.setAdapter( adAdapter );
adAdapter.loadAds();
}
@Override
public void onDestroy()
{
adAdapter.destroy();
super.onDestroy();
}

4. 配置广告渲染设置

在加载广告前,调用 ad plcer 上的 setNativeAdViewBinder。 请始终设置广告尺寸,以优化渲染。

5. 支持设置监听器 (可选)

广告投放器支持可选的监听器,用于通知您事件:

  • onAdLoaded()
  • onAdRemoved()
  • onAdClicked()
  • onAdRevenuePaid()