在 Android (Java) 上从 Google AdMob (ex. AdMob) 迁移到 Yandex 聚合

要在 Android 上使用 Yandex Ads 聚合替换 Google AdMob (ex. AdMob) 广告,请对您的代码进行以下更改。

集成SDK

将 Yandex Mobile Ads SDK 的依赖项添加到应用级别的模块的 Gradle 文件中(通常为 app/build.gradle)。

dependencies {
    implementation 'com.google.android.gms:play-services-ads:22.5.0'
}
dependencies {
    implementation 'com.yandex.android:mobileads:6.2.0'
    implementation 'com.yandex.ads.mediation:mobileads-admob:22.5.0.0'
    implementation 'com.google.android.gms:play-services-ads:22.5.0'
}

在加载广告之前,使用 initialize() 方法初始化库:

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
}
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new InitializationListener() {
            @Override
            public void onInitializationCompleted() {
            }
        });
    }
}

广告格式

插屏广告

加载广告

// ...
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;

public class MainActivity extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);

      MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
      });
      AdRequest adRequest = new AdRequest.Builder().build();

      InterstitialAd.load(this,"ca-app-pub-3940256099942544/1033173712", adRequest,
        new InterstitialAdLoadCallback() {
      @Override
      public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
        // mInterstitialAd 引用的值将为 null,直到
        // 广告完成加载。
        mInterstitialAd = interstitialAd;
        Log.i(TAG, "onAdLoaded");
      }

      @Override
      public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
        // 处理错误
        Log.i(TAG, loadAdError.getMessage());
        mInterstitialAd = null;
      }
    });
  }
}
// ...
import com.yandex.mobile.ads.interstitial.InterstitialAd;
import com.yandex.mobile.ads.interstitial.InterstitialAdEventListener;

public class MainActivity extends Activity {

    private InterstitialAd mInterstitialAd;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new InitializationListener() {
            @Override
            public void onInitializationCompleted() {
            }
        });
        final AdRequest adRequest = new AdRequest.Builder().build();

        mInterstitialAd = new InterstitialAd(this);
        mInterstitialAd.setAdUnitId("demo-interstitial-yandex");
        mInterstitialAd.loadAd(adRequest);
    }
}

设置广告回调

在展示广告之前,设置回调以跟踪与广告相关的事件。

mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
  @Override
  public void onAdDismissedFullScreenContent() {
    // 当全屏内容被关闭时调用。
    Log.d("TAG", "The ad was dismissed.");
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // 当全屏内容无法显示时调用。
    Log.d("TAG", "The ad failed to show.");
  }

  @Override
  public void onAdShowedFullScreenContent() {
    // 当全屏内容显示时调用。
    // 确保将引用设置为 null,
    // 以免再次显示。
    mInterstitialAd = null;
    Log.d("TAG", "The ad was shown.");
  }
});
mInterstitialAd.setInterstitialAdEventListener(new InterstitialAdEventListener() {
    @Override
    public void onAdLoaded() {
        Log.i(TAG, "onAdLoaded");
    }

    @Override
    public void onAdFailedToLoad(@NonNull AdRequestError error) {
        // 处理错误
        Log.i(TAG, error.getDescription());
    }

    @Override
    public void onAdDismissed() {
       // 当插屏广告已关闭时调用。
       Log.d("TAG", "The ad was dismissed.");
    }

    @Override
    public void onAdShown() {
      // 当插屏广告展示时调用。
      Log.d("TAG", "The ad was shown.");
    }

    @Override
    public void onImpression(@Nullable final ImpressionData impressionData) {
         // 当跟踪到一次展示时调用。
       Log.d("TAG", "The ad imprassion was tracked.");
    }

    @Override
    public void onAdClicked() {
        // 当用户点击广告时调用。
      Log.d("TAG", "The ad was clicked.");
    }

    @Override
    public void onReturnedToApplication() {
    // 当用户点击后返回应用程序时调用。
      Log.d("TAG", "The ad was clicked.");
    }

    @Override
    public void onLeftApplication() {
// 当用户点击广告后即将离开应用时调用。
    Log.d("TAG", "The ad left application after click.");
    }
});

显示广告

插屏广告应在应用运行的自然暂停期间展示。例如,在游戏关卡之间或用户完成任务后。要显示插屏广告,请使用 show() 方法。

if (mInterstitialAd != null) {
  mInterstitialAd.show(MyActivity.this);
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
if (mInterstitialAd.isLoaded()) {
  mInterstitialAd.show();
} else {
  Log.d("TAG", "The interstitial ad wasn't ready yet.");
}

将 AdView 对象添加到布局中

要显示横幅,请将“BannerAdView”放置在适当的活动或片段中。为此,请将其添加到相应的布局 XML 文件中:

# main_activity.xml
...
  <com.google.android.gms.ads.AdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true"
      ads:adSize="BANNER"
      ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
  </com.google.android.gms.ads.AdView>
...
# main_activity.xml
...
  <com.yandex.mobile.ads.banner.BannerAdView
      xmlns:ads="http://schemas.android.com/apk/res-auto"
      android:id="@+id/adView"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_centerHorizontal="true"
      android:layout_alignParentBottom="true">
  </com.yandex.mobile.ads.banner.BannerAdView>
...

在代码中指定所需的属性:

// ...
mBannerAdView = (BannerAdView) findViewById(R.id.adView);
mBannerAdView.setAdUnitId("demo-banner-yandex");
mBannerAdView.setAdSize(AdSize.stickySize(context, 320));
// ...
  • adSize:所需的 横幅尺寸

  • adUnitId:广告单元唯一 ID。如果您应用中的横幅显示在活动的不同实例中,我们建议在 Partner Interface 中为每个横幅创建单独的广告单元 ID。

您还可以通过编程方式创建 BannerAdView 对象:

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
// 待办:将 adView 添加到您的视图层次结构中。
BannerAdView adView = new BannerAdView(this);
adView.setAdSize(AdSize.stickySize(context, 320));
adView.setAdUnitId("demo-banner-yandex");
// 待办:将 adView 添加到您的视图层次结构中。

加载广告

package ...

import ...
import com.google.android.gms.ads.adRequest;
import com.google.android.gms.ads.adView;

public class MainActivity extends AppCompatActivity {
private AdView mAdView;

protected void  onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
}
}
package ...

import ...
import com.yandex.mobile.ads.banner.BannerAdEventListener;
import com.yandex.mobile.ads.banner.BannerAdView;

public class MainActivity extends AppCompatActivity {
private BannerAdView mAdView;

protected void  onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MobileAds.initialize(this, new InitializationListener() {
            @Override
            public void onInitializationCompleted() {
            }
        });
        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
}
}

订阅广告展示事件

要自定义广告的行为,您可以订阅广告生命周期事件。

mAdView.setAdListener(new AdListener() {
    @Override
    public void onAdLoaded() {
    // 广告加载完成时执行的代码。
  }
    @Override
    public void onAdFailedToLoad(LoadAdError adError) {
    // 广告请求失败时执行的代码。
    }
    @Override
    public void onAdOpened() {
    // 广告打开覆盖整个屏幕的叠加层时
    //执行的代码。
    }
    @Override
    public void onAdClicked() {
    // 用户点击广告时执行的代码。
    }
    @Override
    public void onAdClosed() {
    // 用户在点击广告后即将返回
      // 应用时执行的代码。
    }
});
mAdView.setBannerAdEventListener(new BannerAdEventListener() {
  @Override
  public void onAdLoaded() {
     // 广告加载完成时执行的代码。
}
@Override
  public onAdFailedToLoad(@NonNull AdRequestError error) {
     // 广告请求失败时执行的代码。
}
  @Override
  public void onAdClicked() {
      // 用户点击广告时执行的代码。
}
  @Override
  public void onImpression(@Nullable final ImpressionData impressionData) {
      // 当跟踪到一次展示时调用。
  }
  @Override
  public void onLeftApplication() {
      // 当用户点击广告后即将离开应用时调用。
  }
@Override
  public void onReturnedToApplication() {
      // 用户在点击广告后即将返回
      // 应用时执行的代码。
}
});

激励广告

加载激励广告对象

import com.google.android.gms.ads.rewarded.RewardedAd;

public class MainActivity extends Activity {
  private RewardedAd mRewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    AdRequest adRequest = new AdRequest.Builder().build();

    RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917",
      adRequest, new RewardedAdLoadCallback() {
        @Override
        public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
          // 处理错误。
          Log.d(TAG, loadAdError.getMessage());
          mRewardedAd = null;
        }

        @Override
        public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
          mRewardedAd = rewardedAd;
          Log.d(TAG, "Ad was loaded.");
        }
    });
  }
}
...
import com.yandex.mobile.ads.rewarded.RewardedAd;
import com.yandex.mobile.ads.rewarded.RewardedAdEventListener;

public class MainActivity extends Activity {
  private RewardedAd mRewardedAd;
  private final String TAG = "MainActivity";

  @Override
  protected void onCreate(Bundle savedInstanceState) {

    AdRequest adRequest = new AdRequest.Builder().build();

mRewardedAd = new RewardedAd(this);
    mRewardedAd.setAdUnitId("demo-rewarded-yandex");
  }
}

设置广告回调

mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() {
  @Override
  public void onAdShowedFullScreenContent() {
    // 广告显示时调用。
    Log.d(TAG, "Ad was shown.");
  }

  @Override
  public void onAdFailedToShowFullScreenContent(AdError adError) {
    // 广告无法显示调用。
    Log.d(TAG, "Ad failed to show.");
  }

  @Override
  public void onAdDismissedFullScreenContent() {
    // 广告关闭时调用。
    // 将广告引用设置为 null,以免再次显示广告。
    Log.d(TAG, "Ad was dismissed.");
    mRewardedAd = null;
  }
});
mRewardedAd.setRewardedAdEventListener(new RewardedAdEventListener() {    @Override
    public void onAdLoaded() {
        Log.i(TAG, "onAdLoaded");
    }

    @Override
    public void onAdFailedToLoad(@NonNull AdRequestError error) {
        // 处理错误
        Log.i(TAG, error.getDescription());
    }

    public void onRewarded(@NonNull final Reward reward) {
        // 处理奖励。
        Log.d(TAG, "The user earned the reward.");
        int rewardAmount = rewardItem.getAmount();
        String rewardType = rewardItem.getType();
    }

    @Override
    public void onAdDismissed() {
       // 当插屏广告已关闭时调用。
       Log.d("TAG", "The ad was dismissed.");
    }

    @Override
    public void onAdShown() {
       // 当插屏广告展示时调用。
       Log.d("TAG", "The ad was shown.");
    }

    @Override
    public void onImpression(@Nullable final ImpressionData impressionData) {
           // 当跟踪到一次展示时调用。
       Log.d("TAG", "The ad imprassion was tracked.");
    }

    @Override
    public void onAdClicked() {
            // 当用户点击广告时调用。
      Log.d("TAG", "The ad was clicked.");
    }

    @Override
    public void onReturnedToApplication() {
            // 当用户点击后返回应用程序时调用。
      Log.d("TAG", "The ad was clicked.");
    }

    @Override
    public void onLeftApplication() {
            // 当用户点击广告后即将离开应用时调用。
      Log.d("TAG", "The ad left application after click.");
    }
});

显示广告

要显示激励广告,请使用 RewardedAdEventListener 对象来处理奖励事件。

if (mRewardedAd != null) {
  Activity activityContext = MainActivity.this;
  mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() {
    @Override
    public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
      // 处理奖励。
      Log.d(TAG, "The user earned the reward.");
      int rewardAmount = rewardItem.getAmount();
      String rewardType = rewardItem.getType();
    }
  });
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}
if (mRewardedAd.isLoaded()) {
  mRewardedAd.show();
} else {
  Log.d(TAG, "The rewarded ad wasn't ready yet.");
}

联系支持部门