# Android 原生广告服务集成

> 通过加载原生广告、创建和绑定视图以显示广告资源以及在使用后正确销毁广告对象，将原生广告服务集成到 Android app。

原生广告服务是一种可以自定义的广告形式，可与 app 中的其他内容混合。这使得广告服务看起来更有机，从而带来更好的用户体验和更高的留存率。

## 先决条件##prerequisites

确保已将 LevelPlay SDK 7.9.0+ 正确集成到应用程序中。[此处](/grow/levelplay/sdk/android/sdk-integration.md)概述了集成。

## 集成原生广告服务##integrate-native-ads

要集成原生广告服务，您需要遵循三个主要步骤： 

1. 加载原生广告
2. 创建视图并将其绑定
3. 销毁原生广告

## 步骤 1.加载原生广告##step-1.-load-a-native-ad

需要使用 LevelPlayNativeAd.Builder 类创建原生广告对象，该类允许自定义原生广告对象的配置。我们建议创建一个单独的类管理原生广告服务加载机制。 

务必在收到 [SDK 初始化完成](/grow/levelplay/sdk/android/sdk-integration.md)回调后加载原生广告服务。 

```java
    LevelPlayNativeAd nativeAd = new LevelPlayNativeAd.Builder()
            .withPlacementName(placementName)
            .withListener(listener) 
            .build();
// You can add the native ad object to a list of native ads (mNativeAds), which can be used to display the ad when needed
    mNativeAds.add(nativeAd);
    nativeAd.loadAd();
```

您可以使用放置/位置名称初始化原生广告，并为广告回调设置监听器。

> **Note:**
>
> 无论是在展示广告之后还是遇到加载失败，您都需要销毁广告对象并为每次新加载创建一个新对象。

## 步骤 2.实现监听器##step-2.-implement-the-listener

接下来，在代码中实现原生广告监听器。LevelPlay SDK 会触发多个回调来通知您原生广告活动。SDK 将通知监听器以下列出的所有可能事件：

```java
    // Invoked each time a native ad was loaded.
    @Override
    public void onAdLoaded(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
        // Your implementation here
    }
    // Invoked when the native ad loading process has failed.
    @Override
    public void onAdLoadFailed(LevelPlayNativeAd nativeAd, IronSourceError error) {
        // Your implementation here
    }
    // Invoked each time the first pixel is visible on the screen
    @Override
    public void onAdImpression(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
        // Your implementation here
    }
    // Invoked when end user clicked on the native ad
    @Override
    public void onAdClicked(LevelPlayNativeAd nativeAd, AdInfo adInfo) {
        // Your implementation here
    }
```

> **Note:**
>
> 为加载而创建的原生广告对象和回调中返回的原生广告对象引用相同的原生广告对象。

## 步骤 3.创建视图并将其绑定##step-3.-create-a-view-and-bind-it

在加载原生广告之前，您需要创建视图、绑定视图并手动设置所有相关信息。这包括安排不同的资源，例如

作品/游戏作品、正文文本、广告文本、图像、媒体视图容器、CTA 按钮等。 

每个原生广告由单个 NativeAdLayout 表示，此视图负责显示该广告的所有元素。

以下是有关 XML 格式的建议，该格式可用作展示广告的基准：

```java
<com.ironsource.mediationsdk.ads.nativead.NativeAdLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingStart="5dp"
    android:paddingEnd="5dp"
    android:paddingTop="10dp"
    android:contentDescription="com.ironsource.mediationsdk.ads.NativeAdLayout">
    <LinearLayout
        android:orientation="vertical"
        ...>
            <TextView
                android:id="@+id/badge"
                .../>
                <ImageView
                    android:id="@+id/ad_app_icon"
                    ...>
                    <TextView
                        android:id="@+id/ad_title"
                        .../>
                    <按钮
                        android:id="@+id/ad_call_to_action"
                        .../>
             // Other assets, such as media view.
         ...
                </LinearLayout>
</com.ironsource.mediationsdk.ads.nativead.NativeAdLayout>
```

### 提高广告透明度##enhancing-ad-transparency

**隐私图标 -** 在设计过程中，此元素应包含在原生广告的左下角。

**广告指示** - 为了确保您的用户了解这是广告，您需要将原生广告服务标记为“Ad”。这可以通过添加自己的指示来实现。

#### 创建 NativeAdLayout 并将其填充##create-a-nativeadlayout-and-populate-it

#### 放布局局##inflate-the-layout

将布局 XML 文件扩展为 NativeAdLayout 对象，可用于在布局中显示原生广告元素。

```java
    LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    NativeAdLayout nativeAdLayout = (NativeAdLayout) inflater.inflate(R.layout.native_ad, null);
```

#### 资源视图 人口和注册##assets-views-population-and-registration

此代码示例标识用于显示作品/游戏作品、图标和 MediaView 的视图。然后，填充资源并向 nativeAdLayout 对象注册它们：

```java
    // Locate all views to be populated
    TextView titleView = nativeAdLayout.findViewById(R.id.ad_title);
    ImageView iconView = nativeAdLayout.findViewById(R.id.ad_app_icon);
    LevelPlayMediaView mediaView = nativeAdLayout.findViewById(R.id.ad_media);
    // Populate the views
    String title = nativeAd.getTitle();
    if (title != null) {
        titleView.setText(title);
    }
    NativeAdDataInterface.图像图标 = nativeAd.getIcon();
    if (icon != null) {
        if (icon.getDrawable() != null) {
            iconView.setImageDrawable(icon.getDrawable());
        }
    }
    // Register the views to be bound
    nativeAdLayout.setTitleView(titleView);
    nativeAdLayout.setIconView(iconView);
    nativeAdLayout.setMediaView(mediaView);
    nativeAdLayout.setCallToActionView(ctaView);

    // Register the native ad with the layout
    nativeAdLayout.registerNativeAdViews(nativeAd);

```

对于app程序将显示的原生广告对象提供的每个资产，重复查找与该资产对应的视图、设置其值并将其注册到 ad view 类的过程。

**MediaView** 

MediaView 是一个指定容器，用于显示主媒体元素。建议对容器使用固定大小。

## 步骤 4.销毁原生广告##step-4.-destroy-the-native-ad

要销毁原生广告，请调用以下方法：

```java
nativeAd.destroyAd();
```

无法再加载已销毁的原生广告。如果要再次提供服务，必须再次发起。

## 支持的网络##supported-networks

| 网络                        | SDK    | 适配器    |
| ------------------------- | ------ | ------ |
| Google AdMob 和 Ad Manager | 22.1.0 | 4.3.38 |
| Meta                      | 6.14.0 | 4.3.43 |
