在本教程中, 我们在Android应用程序中实施Google AdMob插页式广告。要将Google AdMob放置在Android应用程序中, 我们需要创建Google广告单元ID。有关创建Google AdMod帐户和生成广告单元ID的完整参考, 请参见Android Google AdMob。
插页式广告是覆盖整个活动布局的全屏广告。该广告显示在活动的过渡点。要在Android应用程序中实现Google AdMob, 请选择Google AdMob广告活动, 然后将广告格式类型选择为插页式广告。
我们还可以将Google AdMob广告放置在其他活动(例如空白活动)上。
在build.gradle文件中添加Google广告依赖项“ com.google.android.gms:play-services-ads:17.0.0”:
build.gradle
dependencies {implementation fileTree(dir: 'libs', include: ['*.jar'])implementation"org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"implementation 'com.android.support:appcompat-v7:26.1.0'implementation 'com.google.android.gms:play-services-ads:17.0.0'testImplementation 'junit:junit:4.12'}
activity_main.xml
将你的UI代码添加到activity_main.xml中。 Button组件用于加载广告。
<
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context="example.srcmini.com.kotlininterstitialads.MainActivity">
<
!-- view for AdMob Interstitial Ad -->
<
TextViewandroid:id="@+id/app_title"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_marginTop="50dp"android:text="@string/interstitial_ad_sample"android:textAppearance="?android:attr/textAppearanceLarge" />
<
Buttonandroid:id="@+id/load_ad_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_centerHorizontal="true"android:layout_centerVertical="true"android:text="@string/load_ad" />
<
/RelativeLayout>
strings.xml
在string.xml文件中添加创建的广告单元ID。
<
resources>
<
string name="app_name">
KotlinInterstitialAds<
/string>
<
string name="action_settings">
Settings<
/string>
<
string name="interstitial_ad_sample">
Interstitial Ad Sample<
/string>
<
string name="load_ad">
Load Ad<
/string>
<
!-- -This is an ad unit ID for an interstitial test ad. Replace with your own interstitial ad unit id.-->
<
string name="interstitial_ad_unit_id">
ca-app-pub-3940256099942544/1033173712<
/string>
<
/resources>
MainActivity.kt
在MainActivity.kt类中添加以下代码。要将广告加载到UI上, 请创建InterstitialAd实例, 然后在InterstitialAd interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id)上初始化广告单元ID。
【Kotlin Android Google AdMob非页内广告示例】覆盖InterstitialAd监听器onAdLoaded(), onAdFailedToLoad(), onAdClosed。要在点击按钮时加载广告, 请创建AdRequest实例, 然后通过调用InterstitialAd !!。loadAd(AdRequest)加载广告。
package example.srcmini.com.kotlininterstitialadsimport com.google.android.gms.ads.AdListenerimport com.google.android.gms.ads.AdRequestimport com.google.android.gms.ads.InterstitialAdimport android.os.Bundleimport android.support.v7.app.AppCompatActivityimport android.widget.Buttonimport android.widget.Toastclass MainActivity : AppCompatActivity() {private var mLoadAdButton: Button? = nullprivate var mInterstitialAd: InterstitialAd? = nulloverride fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)setContentView(R.layout.activity_main)// Create the InterstitialAd and set the adUnitId (defined in values/strings.xml).mInterstitialAd = newInterstitialAd()loadInterstitial()// Create the load ad button, tries to show an interstitial when clicked.mLoadAdButton = findViewById(R.id.load_ad_button) as ButtonmLoadAdButton!!.isEnabled = falsemLoadAdButton!!.setOnClickListener {showInterstitial()}}private fun newInterstitialAd(): InterstitialAd {val interstitialAd = InterstitialAd(this)interstitialAd.adUnitId = getString(R.string.interstitial_ad_unit_id)interstitialAd.adListener = object : AdListener() {override fun onAdLoaded() {mLoadAdButton!!.isEnabled = trueToast.makeText(applicationContext, "Ad Loaded", Toast.LENGTH_SHORT).show()}override fun onAdFailedToLoad(errorCode: Int) {mLoadAdButton!!.isEnabled = trueToast.makeText(applicationContext, "Ad Failed To Load", Toast.LENGTH_SHORT).show()}override fun onAdClosed() {// Proceed to the next level.// goToNextLevel()Toast.makeText(applicationContext, "Ad Closed", Toast.LENGTH_SHORT).show()tryToLoadAdOnceAgain()}}return interstitialAd}private fun loadInterstitial() {// Disable the load ad button and load the ad.mLoadAdButton!!.isEnabled = falseval adRequest = AdRequest.Builder().build()mInterstitialAd!!.loadAd(adRequest)}private fun showInterstitial() {// Show the ad if it is ready. Otherwise toast and reload the ad.if (mInterstitialAd != null &
&
mInterstitialAd!!.isLoaded) {mInterstitialAd!!.show()} else {Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show()tryToLoadAdOnceAgain()}}private fun tryToLoadAdOnceAgain() {mInterstitialAd = newInterstitialAd()loadInterstitial()}}
AndroidManifest.xml
在AndroidManifest.xml文件中添加以下代码:
<
?xml version="1.0" encoding="utf-8"?>
<
manifest xmlns:android="http://schemas.android.com/apk/res/android"package="example.srcmini.com.kotlininterstitialads">
<
!-- Include required permissions for Google Mobile Ads to run. -->
<
uses-permission android:name="android.permission.INTERNET" />
<
uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<
applicationandroid:allowBackup="true"android:icon="@mipmap/ic_launcher"android:label="@string/app_name"android:roundIcon="@mipmap/ic_launcher_round"android:supportsRtl="true"android:theme="@style/AppTheme">
<
!-- This meta-data tag is required to use Google Play Services. -->
<
meta-dataandroid:name="com.google.android.gms.version"android:value="http://www.srcmini.com/@integer/google_play_services_version" />
<
activityandroid:name=".MainActivity"android:label="@string/app_name">
<
intent-filter>
<
action android:name="android.intent.action.MAIN" />
<
category android:name="android.intent.category.LAUNCHER" />
<
/intent-filter>
<
/activity>
<
!-- Include the AdActivity configChanges and theme. -->
<
activityandroid:name="com.google.android.gms.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"android:theme="@android:style/Theme.Translucent" />
<
meta-dataandroid:name="com.google.android.gms.ads.AD_MANAGER_APP"android:value="http://www.srcmini.com/true"/>
<
/application>
<
/manifest>
输出:
文章图片
文章图片
文章图片
文章图片
推荐阅读
- Kotlin Android Google AdMob横幅广告示例
- Kotlin Android显式Intent
- Kotlin Android自定义Toast
- Kotlin Android自定义ListView
- Kotlin Android上下文菜单
- Kotlin Android按钮
- Kotlin Android AlertDialog
- Kotlin抽象类
- Jetpack|MAD,现代安卓开发技术(Android 领域开发方式的重大变革~)