Миграция с AdMob на медиацию Яндекса
Это архивная версия документации. Актуальная документация по всем платформам находится здесь.
Чтобы заменить рекламу AdMob на медиацию Yandex Ads на Android, внесите в свой код следующие изменения.
Подключение SDK
Добавьте зависимости для Yandex Mobile Ads SDK в файл Gradle вашего модуля на уровне приложения, обычно app/build.gradle.
AdMob | Yandex Ads |
|
|
AdMob | Yandex Ads |
|
|
Перед загрузкой рекламы инициализируйте библиотеку с помощью метода initialize()
:
AdMob | Yandex Ads |
|
|
AdMob | Yandex Ads |
|
|
AdMob | Yandex Ads |
|
|
AdMob | Yandex Ads |
|
|
Форматы объявлений
Interstitial
- Загрузка объявления
-
AdMob Yandex Ads // ... 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) { // The mInterstitialAd reference will be null until // an ad is loaded. mInterstitialAd = interstitialAd; Log.i(TAG, "onAdLoaded"); } @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Handle the error 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); } }
СкопированоAdMob Yandex Ads // ... 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) { // The mInterstitialAd reference will be null until // an ad is loaded. mInterstitialAd = interstitialAd; Log.i(TAG, "onAdLoaded"); } @Override public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) { // Handle the error 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); } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.interstitial.InterstitialAd import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback class MainActivity : AppCompatActivity() { private var interstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } val adRequest: AdRequest = Builder().build() InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() { override fun onAdLoaded(interstitialAdLoaded: InterstitialAd) { // The interstitialAd reference will be null until // an ad is loaded. interstitialAd = interstitialAdLoaded Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Handle the error Log.i("TAG", loadAdError?.toString()) interstitialAd = null } }) } }
Скопированоimport com.yandex.mobile.ads.common.MobileAds import com.yandex.mobile.ads.interstitial.InterstitialAd import com.yandex.mobile.ads.common.AdRequest class MainActivity : AppCompatActivity() { private var interstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } val adRequest: AdRequest = Builder().build() interstitialAd = InterstitialAd(this) interstitialAd?.setAdUnitId("demo-interstitial-yandex") interstitialAd?.loadAd(adRequest) } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.interstitial.InterstitialAd import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback class MainActivity : AppCompatActivity() { private var interstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } val adRequest: AdRequest = Builder().build() InterstitialAd.load(this, "ca-app-pub-3940256099942544/1033173712", adRequest, object : InterstitialAdLoadCallback() { override fun onAdLoaded(interstitialAdLoaded: InterstitialAd) { // The interstitialAd reference will be null until // an ad is loaded. interstitialAd = interstitialAdLoaded Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Handle the error Log.i("TAG", loadAdError?.toString()) interstitialAd = null } }) } }
Скопированоimport com.yandex.mobile.ads.common.MobileAds import com.yandex.mobile.ads.interstitial.InterstitialAd import com.yandex.mobile.ads.common.AdRequest class MainActivity : AppCompatActivity() { private var interstitialAd: InterstitialAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } val adRequest: AdRequest = Builder().build() interstitialAd = InterstitialAd(this) interstitialAd?.setAdUnitId("demo-interstitial-yandex") interstitialAd?.loadAd(adRequest) } }
Скопировано - Установка колбеков рекламы
-
Перед показом рекламы установите колбеки, чтобы отслеживать события, связанные с вашим объявлением.
AdMob Yandex Ads mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){ @Override public void onAdDismissedFullScreenContent() { // Called when fullscreen content is dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { // Called when fullscreen content failed to show. Log.d("TAG", "The ad failed to show."); } @Override public void onAdShowedFullScreenContent() { // Called when fullscreen content is shown. // Make sure to set your reference to null so you don't // show it a second time. 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) { // Handle the error Log.i(TAG, error.getDescription()); } @Override public void onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown."); } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked."); } @Override public void onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked."); } @Override public void onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked."); } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click."); } });
СкопированоAdMob Yandex Ads mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){ @Override public void onAdDismissedFullScreenContent() { // Called when fullscreen content is dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { // Called when fullscreen content failed to show. Log.d("TAG", "The ad failed to show."); } @Override public void onAdShowedFullScreenContent() { // Called when fullscreen content is shown. // Make sure to set your reference to null so you don't // show it a second time. 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) { // Handle the error Log.i(TAG, error.getDescription()); } @Override public void onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown."); } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked."); } @Override public void onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked."); } @Override public void onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked."); } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click."); } });
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdError import com.google.android.gms.ads.FullScreenContentCallback interstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() { override fun onAdDismissedFullScreenContent() { // Called when fullscreen content is dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdFailedToShowFullScreenContent(adError: AdError) { // Called when fullscreen content failed to show. Log.d("TAG", "The ad failed to show.") } override fun onAdShowedFullScreenContent() { // Called when fullscreen content is shown. // Make sure to set your reference to null so you don't // show it a second time. interstitialAd = null Log.d("TAG", "The ad was shown.") } }
Скопированоimport com.yandex.mobile.ads.common.ImpressionData import com.yandex.mobile.ads.common.AdRequestError interstitialAd?.setInterstitialAdEventListener(object : InterstitialAdEventListener { override fun onAdLoaded() { Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(error: AdRequestError) { // Handle the error Log.i("TAG", error.description) } override fun onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown.") } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked.") } override fun onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked.") } override fun onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked.") } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click.") } })
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdError import com.google.android.gms.ads.FullScreenContentCallback interstitialAd?.fullScreenContentCallback = object : FullScreenContentCallback() { override fun onAdDismissedFullScreenContent() { // Called when fullscreen content is dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdFailedToShowFullScreenContent(adError: AdError) { // Called when fullscreen content failed to show. Log.d("TAG", "The ad failed to show.") } override fun onAdShowedFullScreenContent() { // Called when fullscreen content is shown. // Make sure to set your reference to null so you don't // show it a second time. interstitialAd = null Log.d("TAG", "The ad was shown.") } }
Скопированоimport com.yandex.mobile.ads.common.ImpressionData import com.yandex.mobile.ads.common.AdRequestError interstitialAd?.setInterstitialAdEventListener(object : InterstitialAdEventListener { override fun onAdLoaded() { Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(error: AdRequestError) { // Handle the error Log.i("TAG", error.description) } override fun onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown.") } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked.") } override fun onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked.") } override fun onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked.") } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click.") } })
Скопировано - Показать объявление
-
Межстраничная реклама должна отображаться во время естественных пауз в работе приложения. Например, между уровнями игры или после того, как пользователь выполнил задачу. Чтобы показать межстраничное объявление, используйте метод
show()
.AdMob Yandex Ads 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."); }
СкопированоAdMob Yandex Ads 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."); }
СкопированоAdMob Yandex Ads if (interstitialAd != null) { interstitialAd?.show(MyActivity.this) } else { Log.d("TAG", "The interstitial ad wasn't ready yet.") }
Скопированоif (interstitialAd?.isLoaded == true) { interstitialAd?.show() } else { Log.d("TAG", "The interstitial ad wasn't ready yet.") }
СкопированоAdMob Yandex Ads if (interstitialAd != null) { interstitialAd?.show(MyActivity.this) } else { Log.d("TAG", "The interstitial ad wasn't ready yet.") }
Скопированоif (interstitialAd?.isLoaded == true) { interstitialAd?.show() } else { Log.d("TAG", "The interstitial ad wasn't ready yet.") }
Скопировано
Баннер
- Добавьте AdView в верстку
-
Чтобы отобразить баннер, разместите
BannerAdView
в нужном Activity или Fragment. Для этого добавьте его в соответствующий XML-файл макета:AdMob Yandex Ads # 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> ...
СкопированоAdMob Yandex Ads # 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)); // ...
Скопировано// ... bannerAdView = (BannerAdView) findViewById(R.id.adView) bannerAdView.setAdUnitId("demo-banner-yandex") bannerAdView.setAdSize(AdSize.stickySize(context, 320)) // ...
СкопированоadSize
— необходимый размер для баннера.adUnitId
— уникальный идентификатор рекламного блока. Если в вашем приложении баннер показывается в разных Activity, рекомендуется для каждого баннера создавать отдельные идентификаторы рекламных блоков в Партнерском интерфейсе.
Также вы можете создать
BannerAdView
программно:AdMob Yandex Ads AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // TODO: Add adView to your view hierarchy.
СкопированоBannerAdView adView = new BannerAdView(this); adView.setAdSize(AdSize.stickySize(context, 320)); adView.setAdUnitId("demo-banner-yandex"); // TODO: Add adView to your view hierarchy.
СкопированоAdMob Yandex Ads AdView adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); // TODO: Add adView to your view hierarchy.
СкопированоBannerAdView adView = new BannerAdView(this); adView.setAdSize(AdSize.stickySize(context, 320)); adView.setAdUnitId("demo-banner-yandex"); // TODO: Add adView to your view hierarchy.
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdView val adView = AdView(this) adView.setAdSize(AdSize.BANNER) adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // TODO: Add adView to your view hierarchy.
Скопированоimport com.yandex.mobile.ads.banner.AdSize import com.yandex.mobile.ads.banner.BannerAdView val adView = BannerAdView(this) adView.setAdSize(AdSize.stickySize(context, 320)) adView.setAdUnitId("demo-banner-yandex") // TODO: Add adView to your view hierarchy.
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdSize import com.google.android.gms.ads.AdView val adView = AdView(this) adView.setAdSize(AdSize.BANNER) adView.adUnitId = "ca-app-pub-3940256099942544/6300978111" // TODO: Add adView to your view hierarchy.
Скопированоimport com.yandex.mobile.ads.banner.AdSize import com.yandex.mobile.ads.banner.BannerAdView val adView = BannerAdView(this) adView.setAdSize(AdSize.stickySize(context, 320)) adView.setAdUnitId("demo-banner-yandex") // TODO: Add adView to your view hierarchy.
Скопировано - Загрузка рекламы
-
AdMob Yandex Ads 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); } }
СкопированоAdMob Yandex Ads 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); } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdView import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.initialization.InitializationStatus class MainActivity : AppCompatActivity() { private var adView: AdView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } adView = findViewById<AdView>(R.id.adView) val adRequest = AdRequest.Builder().build() adView?.loadAd(adRequest) } }
Скопированоimport com.yandex.mobile.ads.banner.BannerAdView import com.yandex.mobile.ads.common.AdRequest import com.yandex.mobile.ads.common.MobileAds class MainActivity : AppCompatActivity() { private var adView: BannerAdView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } adView = findViewById<BannerAdView>(R.id.adView) val adRequest: AdRequest = Builder().build() adView?.loadAd(adRequest) } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.AdView import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.initialization.InitializationStatus class MainActivity : AppCompatActivity() { private var adView: AdView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } adView = findViewById<AdView>(R.id.adView) val adRequest = AdRequest.Builder().build() adView?.loadAd(adRequest) } }
Скопированоimport com.yandex.mobile.ads.banner.BannerAdView import com.yandex.mobile.ads.common.AdRequest import com.yandex.mobile.ads.common.MobileAds class MainActivity : AppCompatActivity() { private var adView: BannerAdView? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } adView = findViewById<BannerAdView>(R.id.adView) val adRequest: AdRequest = Builder().build() adView?.loadAd(adRequest) } }
Скопировано - Подписка на события отображения рекламы
-
Чтобы настраивать поведение рекламы, вы можете подписаться на события жизненного цикла рекламных объявлений.
AdMob Yandex Ads mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(LoadAdError adError) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. } @Override public void onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } });
СкопированоmAdView.setBannerAdEventListener(new BannerAdEventListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public onAdFailedToLoad(@NonNull AdRequestError error) { // Code to be executed when an ad request fails. } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. } @Override public void onReturnedToApplication() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } });
СкопированоAdMob Yandex Ads mAdView.setAdListener(new AdListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public void onAdFailedToLoad(LoadAdError adError) { // Code to be executed when an ad request fails. } @Override public void onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. } @Override public void onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } });
СкопированоmAdView.setBannerAdEventListener(new BannerAdEventListener() { @Override public void onAdLoaded() { // Code to be executed when an ad finishes loading. } @Override public onAdFailedToLoad(@NonNull AdRequestError error) { // Code to be executed when an ad request fails. } @Override public void onAdClicked() { // Code to be executed when the user clicks on an ad. } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. } @Override public void onReturnedToApplication() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } });
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.LoadAdError adView?.adListener = object : AdListener() { override fun onAdLoaded() { // Code to be executed when an ad finishes loading. } override fun onAdFailedToLoad(adError: LoadAdError) { // Code to be executed when an ad request fails. } override fun onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } override fun onAdClicked() { // Code to be executed when the user clicks on an ad. } override fun onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } }
Скопированоimport com.yandex.mobile.ads.common.AdRequestError import com.yandex.mobile.ads.common.ImpressionData adView?.setBannerAdEventListener(object : BannerAdEventListener { override fun onAdLoaded() { // Code to be executed when an ad finishes loading. } override fun onAdFailedToLoad(error: AdRequestError) { // Code to be executed when an ad request fails. } override fun onAdClicked() { // Code to be executed when the user clicks on an ad. } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. } override fun onReturnedToApplication() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } })
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.LoadAdError adView?.adListener = object : AdListener() { override fun onAdLoaded() { // Code to be executed when an ad finishes loading. } override fun onAdFailedToLoad(adError: LoadAdError) { // Code to be executed when an ad request fails. } override fun onAdOpened() { // Code to be executed when an ad opens an overlay that // covers the screen. } override fun onAdClicked() { // Code to be executed when the user clicks on an ad. } override fun onAdClosed() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } }
Скопированоimport com.yandex.mobile.ads.common.AdRequestError import com.yandex.mobile.ads.common.ImpressionData adView?.setBannerAdEventListener(object : BannerAdEventListener { override fun onAdLoaded() { // Code to be executed when an ad finishes loading. } override fun onAdFailedToLoad(error: AdRequestError) { // Code to be executed when an ad request fails. } override fun onAdClicked() { // Code to be executed when the user clicks on an ad. } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. } override fun onReturnedToApplication() { // Code to be executed when the user is about to return // to the app after tapping on an ad. } })
Скопировано
Реклама с вознаграждением
- Загрузка объекта рекламы с вознаграждением
-
AdMob Yandex Ads 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) { // Handle the error. 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"); } }
СкопированоAdMob Yandex Ads 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) { // Handle the error. 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"); } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.initialization.InitializationStatus import com.google.android.gms.ads.rewarded.RewardedAd import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback class MainActivity : AppCompatActivity() { private var rewardedAd: RewardedAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } val adRequest: AdRequest = Builder().build() RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() { override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Handle the error. Log.d("TAG", loadAdError?.toString()) rewardedAd = null } override fun onAdLoaded(rewardedAd: RewardedAd) { rewardedAd = rewardedAd Log.d("TAG", "Ad was loaded.") } }) } }
Скопированоimport com.yandex.mobile.ads.common.AdRequest import com.yandex.mobile.ads.common.MobileAds import com.yandex.mobile.ads.rewarded.RewardedAd class MainActivity : AppCompatActivity() { private var rewardedAd: RewardedAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } val adRequest: AdRequest = Builder().build() rewardedAd = RewardedAd(this) rewardedAd?.setAdUnitId("demo-rewarded-yandex") } }
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.LoadAdError import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.initialization.InitializationStatus import com.google.android.gms.ads.rewarded.RewardedAd import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback class MainActivity : AppCompatActivity() { private var rewardedAd: RewardedAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { fun onInitializationComplete(initializationStatus: InitializationStatus?) { } } val adRequest: AdRequest = Builder().build() RewardedAd.load(this, "ca-app-pub-3940256099942544/5224354917", adRequest, object : RewardedAdLoadCallback() { override fun onAdFailedToLoad(loadAdError: LoadAdError) { // Handle the error. Log.d("TAG", loadAdError?.toString()) rewardedAd = null } override fun onAdLoaded(rewardedAd: RewardedAd) { rewardedAd = rewardedAd Log.d("TAG", "Ad was loaded.") } }) } }
Скопированоimport com.yandex.mobile.ads.common.AdRequest import com.yandex.mobile.ads.common.MobileAds import com.yandex.mobile.ads.rewarded.RewardedAd class MainActivity : AppCompatActivity() { private var rewardedAd: RewardedAd? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) MobileAds.initialize(this) { } val adRequest: AdRequest = Builder().build() rewardedAd = RewardedAd(this) rewardedAd?.setAdUnitId("demo-rewarded-yandex") } }
Скопировано - Установка колбеков рекламы
-
AdMob Yandex Ads mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() { @Override public void onAdShowedFullScreenContent() { // Called when ad is shown. Log.d(TAG, "Ad was shown."); } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { // Called when ad fails to show. Log.d(TAG, "Ad failed to show."); } @Override public void onAdDismissedFullScreenContent() { // Called when ad is dismissed. // Set the ad reference to null so you don't show the ad a second time. 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) { // Handle the error Log.i(TAG, error.getDescription()); } public void onRewarded(@NonNull final Reward reward) { // Handle the reward. Log.d(TAG, "The user earned the reward."); int rewardAmount = rewardItem.getAmount(); String rewardType = rewardItem.getType(); } @Override public void onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown."); } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked."); } @Override public void onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked."); } @Override public void onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked."); } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click."); } });
СкопированоAdMob Yandex Ads mRewardedAd.setFullScreenContentCallback(new FullScreenContentCallback() { @Override public void onAdShowedFullScreenContent() { // Called when ad is shown. Log.d(TAG, "Ad was shown."); } @Override public void onAdFailedToShowFullScreenContent(AdError adError) { // Called when ad fails to show. Log.d(TAG, "Ad failed to show."); } @Override public void onAdDismissedFullScreenContent() { // Called when ad is dismissed. // Set the ad reference to null so you don't show the ad a second time. 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) { // Handle the error Log.i(TAG, error.getDescription()); } public void onRewarded(@NonNull final Reward reward) { // Handle the reward. Log.d(TAG, "The user earned the reward."); int rewardAmount = rewardItem.getAmount(); String rewardType = rewardItem.getType(); } @Override public void onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed."); } @Override public void onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown."); } @Override public void onImpression(@Nullable final ImpressionData impressionData) { // Called when an impression was tracked Log.d("TAG", "The ad imprassion was tracked."); } @Override public void onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked."); } @Override public void onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked."); } @Override public void onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click."); } });
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.AdError rewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() { override fun onAdShowedFullScreenContent() { // Called when ad is shown. Log.d(TAG, "Ad was shown.") } override fun onAdFailedToShowFullScreenContent(adError: AdError) { // Called when ad fails to show. Log.d(TAG, "Ad failed to show.") } override fun onAdDismissedFullScreenContent() { // Called when ad is dismissed. // Set the ad reference to null so you don't show the ad a second time. Log.d(TAG, "Ad was dismissed.") } }
Скопированоimport com.yandex.mobile.ads.rewarded.RewardedAdEventListener import com.yandex.mobile.ads.common.AdRequestError import com.yandex.mobile.ads.rewarded.Reward rewardedAd?.setRewardedAdEventListener(object : RewardedAdEventListener { override fun onAdLoaded() { Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(error: AdRequestError) { // Handle the error Log.i("TAG", error.description) } override fun onRewarded(reward: Reward) { // Handle the reward. Log.d("TAG", "The user earned the reward.") val rewardAmount: Int = reward.amount val rewardType: String = reward.type } override fun onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown.") } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked Log.d("TAG", "The ad impression was tracked.") } override fun onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked.") } override fun onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked.") } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click.") } })
СкопированоAdMob Yandex Ads import com.google.android.gms.ads.FullScreenContentCallback import com.google.android.gms.ads.AdError rewardedAd?.fullScreenContentCallback = object : FullScreenContentCallback() { override fun onAdShowedFullScreenContent() { // Called when ad is shown. Log.d(TAG, "Ad was shown.") } override fun onAdFailedToShowFullScreenContent(adError: AdError) { // Called when ad fails to show. Log.d(TAG, "Ad failed to show.") } override fun onAdDismissedFullScreenContent() { // Called when ad is dismissed. // Set the ad reference to null so you don't show the ad a second time. Log.d(TAG, "Ad was dismissed.") } }
Скопированоimport com.yandex.mobile.ads.rewarded.RewardedAdEventListener import com.yandex.mobile.ads.common.AdRequestError import com.yandex.mobile.ads.rewarded.Reward rewardedAd?.setRewardedAdEventListener(object : RewardedAdEventListener { override fun onAdLoaded() { Log.i("TAG", "onAdLoaded") } override fun onAdFailedToLoad(error: AdRequestError) { // Handle the error Log.i("TAG", error.description) } override fun onRewarded(reward: Reward) { // Handle the reward. Log.d("TAG", "The user earned the reward.") val rewardAmount: Int = reward.amount val rewardType: String = reward.type } override fun onAdDismissed() { // Called when an interstitial ad has been dismissed. Log.d("TAG", "The ad was dismissed.") } override fun onAdShown() { // Called when an interstitial ad has been shown. Log.d("TAG", "The ad was shown.") } override fun onImpression(impressionData: ImpressionData?) { // Called when an impression was tracked Log.d("TAG", "The ad impression was tracked.") } override fun onAdClicked() { // Called when user clicked on the ad. Log.d("TAG", "The ad was clicked.") } override fun onReturnedToApplication() { // Called when user returned to application after click. Log.d("TAG", "The ad was clicked.") } override fun onLeftApplication() { // Called when user is about to leave application after tapping on an ad. Log.d("TAG", "The ad left application after click.") } })
Скопировано - Показать объявление
-
Когда вы показываете рекламу с вознаграждением, используйте объект
RewardedAdEventListener
для обработки события вознаграждения.AdMob Yandex Ads if (mRewardedAd != null) { Activity activityContext = MainActivity.this; mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { // Handle the reward. 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."); }
СкопированоAdMob Yandex Ads if (mRewardedAd != null) { Activity activityContext = MainActivity.this; mRewardedAd.show(activityContext, new OnUserEarnedRewardListener() { @Override public void onUserEarnedReward(@NonNull RewardItem rewardItem) { // Handle the reward. 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."); }
СкопированоAdMob Yandex Ads rewardedAd?.let { ad -> ad.show(this, OnUserEarnedRewardListener { rewardItem -> // Handle the reward. val rewardAmount = rewardItem.amount val rewardType = rewardItem.type Log.d(TAG, "User earned the reward.") }) } ?: run { Log.d(TAG, "The rewarded ad wasn't ready yet.") }
Скопированоif (rewardedAd?.isLoaded == true) { rewardedAd?.show() } else { Log.d("TAG", "The rewarded ad wasn't ready yet.") }
СкопированоAdMob Yandex Ads rewardedAd?.let { ad -> ad.show(this, OnUserEarnedRewardListener { rewardItem -> // Handle the reward. val rewardAmount = rewardItem.amount val rewardType = rewardItem.type Log.d(TAG, "User earned the reward.") }) } ?: run { Log.d(TAG, "The rewarded ad wasn't ready yet.") }
Скопированоif (rewardedAd?.isLoaded == true) { rewardedAd?.show() } else { Log.d("TAG", "The rewarded ad wasn't ready yet.") }
Скопировано