📜  Kotlin Android Google AdMob非页内广告示例

📅  最后修改于: 2021-01-05 08:48:15             🧑  作者: Mango

Kotlin Android Google AdMob非页内广告示例

在本教程中,我们在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组件用于加载广告。



    
    

    

字符串.xml

将创建的广告单元ID添加到字符串.xml文件中。


    KotlinInterstitialAds
    Settings
    Interstitial Ad Sample
    Load Ad
    
    ca-app-pub-3940256099942544/1033173712

MainActivity.kt

在MainActivity.kt类中添加以下代码。加载UI上的广告,创建在InterstitialAd的实例,并初始化上在InterstitialAd interstitialAd.adUnitId =的getString(R。字符串.interstitial_ad_unit_id)的广告单元ID。

覆盖InterstitialAd监听器onAdLoaded(), onAdFailedToLoad(),onAdClosed 。要在点击按钮时加载广告,请创建AdRequest实例,然后通过调用InterstitialAd !!。loadAd(AdRequest)加载广告。

package example.javatpoint.com.kotlininterstitialads

import com.google.android.gms.ads.AdListener
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.InterstitialAd
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {
    private var mLoadAdButton: Button? = null
    private var mInterstitialAd: InterstitialAd? = null

    override 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 Button
        mLoadAdButton!!.isEnabled = false
        mLoadAdButton!!.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 = true
                Toast.makeText(applicationContext, "Ad Loaded", Toast.LENGTH_SHORT).show()
            }

            override fun onAdFailedToLoad(errorCode: Int) {
                mLoadAdButton!!.isEnabled = true
                Toast.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 = false
        val 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文件中添加以下代码:



    
    
    

     
        

        
            
                

                
            
         
        
        
    


输出: