2023-04-16

【Kotlin】AdMob 插頁式廣告(InterstitialAd AdMob20[含]↑)

參考資料 ----

插页式广告

InterstitialAd

不允許的插頁式廣告導入方式

 

 

app 層級的 build.gradle 

 
dependencies {
    ...
    ...
    
    // adMob
    implementation 'com.google.android.gms:play-services-ads:22.0.0'
}

apply plugin: 'com.google.gms.google-services'
 

 

 

AndroidManifest.xml 

 
<application
    ...
    ...>
    
    <meta-data
        android:name="com.google.android.gms.ads.APPLICATION_ID"
        android:value="@string/admob_app_id" />

    ...
    ...
 

 

 

string.xml 

 
<!-- adMob app id 測試 -->
<string name="admob_app_id">ca-app-pub-3940256099942544~3347511713</string>
<!-- 橫幅 測試 -->
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>;
<!-- 插頁 測試 -->
<string name="interstitial_ad_unit_id">ca-app-pub-3940256099942544/8691691433</string>
<!-- 影片 測試 -->
<string name="video_ad_id">ca-app-pub-3940256099942544/5224354917</string>
 

 

 

MainActivity.kt 

 
...
...
import com.google.android.gms.ads.AdError
import com.google.android.gms.ads.AdRequest
import com.google.android.gms.ads.FullScreenContentCallback
import com.google.android.gms.ads.interstitial.InterstitialAd
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback



class MyActivity : Activity()
{
    var mInterstitialAd: InterstitialAd? = null
    
    ...
    ...
    
    override fun onResume()
    {
        var adRequest = AdRequest.Builder().build()
        InterstitialAd.load(this,getString(R.string.interstitial_ad_unit_id), adRequest, object : InterstitialAdLoadCallback() {
            override fun onAdFailedToLoad(adError: LoadAdError) {
                Log.d(TAG, adError.toString())
                mInterstitialAd = null
            }

            override fun onAdLoaded(interstitialAd: InterstitialAd) {
                Log.d(TAG, "Ad was loaded.")
                mInterstitialAd = interstitialAd
            }
        })
        super.onResume()
    }
    
    
    // 當 usser 按了按鈕時
    fun onImgClicked(vv: View)
    {
        toWatchIntersticialAd()
    }
    
    
    // 觀看插頁廣告
    private fun toWatchIntersticialAd()
    {
        val random = Random()
        if ((mInterstitialAd!=null) && (random.nextInt(3)==0))
        // if(mInterstitialAd!= null) // 測試用, 每次都播放
            {
                // 若廣告已載入, 且亂數是 3 的倍數, 則播放
                // (AdMob 規定: 插頁播放的比例須 < 50%)
                mInterstitialAd?.fullScreenContentCallback =
                    object : FullScreenContentCallback() {
                        override fun onAdDismissedFullScreenContent() {
                            Log.d(TAG, "Ad was dismissed.")
                            // user 關閉廣告, 回主畫面
                            mInterstitialAd = null
                            // 返回主畫面
                            NavUtils.navigateUpFromSameTask(this@MyActivity)
                        }

                        override fun onAdFailedToShowFullScreenContent(adError: AdError) {
                            Log.d(TAG, "Ad failed to show.")
                            mInterstitialAd = null
                            // 返回主畫面
                            NavUtils.navigateUpFromSameTask(this@MyActivity)
                        }

                        override fun onAdShowedFullScreenContent() {
                            Log.d(TAG, "Ad showed fullscreen content.")
                            // Called when ad is dismissed.
                        }
                    }
                mInterstitialAd!!.show(this)
            }
        else
            // 直接返回主畫面
            NavUtils.navigateUpFromSameTask(this)
    }
}
 


 

 

 

 

沒有留言:

張貼留言