AdMob Android谷歌廣告接入(精簡版)
import android.app.Activity import android.util.Log import android.widget.Toast import com.google.android.gms.ads.AdListener import com.google.android.gms.ads.AdRequest import com.google.android.gms.ads.InterstitialAd import com.google.android.gms.ads.MobileAds import com.google.android.gms.ads.reward.RewardItem import com.google.android.gms.ads.reward.RewardedVideoAd import com.google.android.gms.ads.reward.RewardedVideoAdListener import com.wj.R /** * @time 2018/6/11 * @author JunJieW * @since
[email protected] * @description 谷歌廣告載入封裝工具類 */ class LoadGoogleAd { private val TAG = "LoadGoogleAd" private var activity: Activity? = null internal var mInterstitialAd: InterstitialAd? = null lateinit var mRewardedVideoAd: RewardedVideoAd constructor(activity: Activity, adType: String) { this.activity = activity //google ad 初始化 MobileAds.initialize(activity,activity.resources.getString(R.string.google_app_id)) if (adType == "inster") { // 插屏ad mInterstitialAd = InterstitialAd(activity) initInterstitialAdListener() mInterstitialAd!!.adUnitId = activity.resources.getString(R.string.google_ad_interstitial_unit_id) mInterstitialAd!!.loadAd( AdRequest.Builder().build()) Log.d(TAG, "The InterstitialAd") } else if (adType == "video") { // 視訊ad mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(activity) initVideoAdListener() mRewardedVideoAd.loadAd(activity.resources.getString(R.string.google_ad_video_unit_id), AdRequest.Builder().build()) Log.d(TAG, "The VideoAd") } } private fun initVideoAdListener() { mRewardedVideoAd.rewardedVideoAdListener = object :RewardedVideoAdListener{ override fun onRewarded(reward: RewardItem) { Toast.makeText(activity, "onRewarded! currency: ${reward.type} amount: ${reward.amount}", Toast.LENGTH_SHORT).show() // Reward the user.做獎勵使用者的操作 } override fun onRewardedVideoAdLeftApplication() { Log.d(TAG, "onRewardedVideoAdLeftApplication") } override fun onRewardedVideoAdClosed() { Log.d(TAG, "onRewardedVideoAdClosed") mRewardedVideoAd.loadAd(activity!!.resources.getString(R.string.google_ad_video_unit_id), AdRequest.Builder().build()) } override fun onRewardedVideoAdFailedToLoad(errorCode: Int) { Log.d(TAG, "onRewardedVideoAdFailedToLoad") } override fun onRewardedVideoAdLoaded() { Log.d(TAG, "onRewardedVideoAdLoaded") } override fun onRewardedVideoAdOpened() { Log.d(TAG, "onRewardedVideoAdOpened") } override fun onRewardedVideoStarted() { Log.d(TAG, "onRewardedVideoStarted") } override fun onRewardedVideoCompleted() { Log.d(TAG, "onRewardedVideoCompleted") } } } private fun initInterstitialAdListener() { //記錄插屏廣告展示的次數,每間隔三次才進行展示 var showInterstitialAdNum = SharedPreferencesUnitls.getParam2(activity,"showInterstitialAdNum") if (StringUtils.StringIsNull(showInterstitialAdNum)){ }else{ SharedPreferencesUnitls.setParam(activity,"showInterstitialAdNum","1") } mInterstitialAd!!.adListener = object: AdListener() { override fun onAdLoaded() { Log.d(TAG, "The interstitial onAdLoaded") } override fun onAdFailedToLoad(errorCode: Int) { Log.d(TAG, "The interstitial onAdFailedToLoad;errorCode==$errorCode") } override fun onAdOpened() { Log.d(TAG, "The interstitial onAdOpened") } override fun onAdLeftApplication() { Log.d(TAG, "The interstitial onAdLeftApplication") } override fun onAdClosed() { Log.d(TAG, "The interstitial onAdClosed") mInterstitialAd!!.loadAd(AdRequest.Builder().build()) } } } //Show the ad 插屏廣告 fun showInterstitialAd() { //記錄插屏廣告展示的次數,每間隔三次才進行展示 var showInterstitialAdNum = SharedPreferencesUnitls.getParam2(activity,"showInterstitialAdNum") if (StringUtils.StringIsNull(showInterstitialAdNum)){ if (showInterstitialAdNum.toInt()%4==0){ if (mInterstitialAd!!.isLoaded){ mInterstitialAd!!.show() } } SharedPreferencesUnitls.setParam(activity,"showInterstitialAdNum",(showInterstitialAdNum.toInt()+1).toString()) }else{ SharedPreferencesUnitls.setParam(activity,"showInterstitialAdNum","1") } } //Show the ad video廣告 fun showRewardedVideoAd(){ //記錄插屏廣告展示的次數,每間隔三次才進行展示 var showInterstitialAdNum = SharedPreferencesUnitls.getParam2(activity,"showVideoAdNum") if (StringUtils.StringIsNull(showInterstitialAdNum)){ if (showInterstitialAdNum.toInt()%4==0){ if (mRewardedVideoAd.isLoaded) { mRewardedVideoAd.show() } } SharedPreferencesUnitls.setParam(activity,"showVideoAdNum",(showInterstitialAdNum.toInt()+1).toString()) }else{ SharedPreferencesUnitls.setParam(activity,"showVideoAdNum","1") } } }
呼叫關鍵程式碼:
//第一步 宣告工具類變數 private LoadGoogleAd googleAdUti_intes = null; private LoadGoogleAd googleAdUti_video = null; //第二步 在onCreate方法中初始化,傳遞廣告型別的標識 googleAdUti_intes = new LoadGoogleAd(getActivity(), "inster"); googleAdUti_video = new LoadGoogleAd(getActivity(), "video"); //第三步 在你想展示廣告的地方呼叫展示廣告的方法 //載入開啟文章的google插屏廣告 googleAdUti_intes.showInterstitialAd(); //載入的google視訊廣告 googleAdUti_video.showRewardedVideoAd();
至此載入谷歌廣告就已經不是問題了!
插屏廣告載入效果圖:
視訊廣告載入效果圖:
最後值得一提的是,谷歌廣告官方建議為了客戶體驗好,載入廣告的時候不用太長時間,避免使用者不愉快體驗,應該在需要呼叫展示廣告的activity或者fragment中,提前將廣告請求建立,只不過不show出來,然後當你需要展示的時候只需要判斷當前廣告物件是否是載入完成狀態,如果是則show出來,這樣速度很快,我做時就被這裡坑過,當用戶點選列表的一個item時才初始化一個廣告請求,然後載入一個插屏廣告,竟然耗時能超過3秒多有時候,體驗巨差,自己都不能忍,後來優化為現在的activity或者fragment建立時就初始化廣告請求只是不show出來,當需要展示的時候直接show方法呼叫就行,還要記得在廣告關閉的時候重新發起下一個廣告請求的建立,只是不show展示出來就行~
過了一段時間,再做別的專案的時候又遇到了一些新的問題,也挺坑的,在這裡補充一下作為爬坑記錄,以防長時間不用了再遇到忘記。
異常1:W/Ads: Failed to load ad: errorcode == 3
完整的異常日誌輸出:
09-12 11:02:43.932 7914-7914/com.mobile I/Ads: Starting ad request.
09-12 11:02:43.932 7914-7914/com.mobile I/Ads: This request is sent from a test device.
09-12 11:02:44.067 7914-8007/com.mobile W/Ads: Invoke Firebase method getInstance error.
09-12 11:02:44.067 7914-8007/com.mobile W/Ads: The Google Mobile Ads SDK will not integrate with Firebase. Admob/Firebase integration requires the latest Firebase SDK jar, but Firebase SDK is either missing or out of date
09-12 11:02:45.596 7914-8016/com.mobile I/Ads: No fill from ad server.
09-12 11:02:49.336 7914-7914/com.mobile W/Ads: Failed to load ad: 3
造成原因:這個錯誤的描述是:ERROR_CODE_NO_FILL,廣告沒有填充。而造成的原因可能是:
1.正式版的沒有釋出,在app沒有上線之前,都是測試的廣告位id,沒有正式的廣告位id,所以可以直接使用官方給的測試廣告位id,這裡附上官方連結:https://developers.google.com/admob/android/test-ads
解決方案:使用官方給的測試廣告位id,這裡附上各種廣告型別的官方給的測試id的截圖,可以各自對號入座找自己適合的,我這裡是Rewarded Video的
// 修改前
mRewardedVideoAd!!.loadAd(resources.getString(R.string.google_ad_video_unit_id), AdRequest.Builder()
// 修改後 把這裡的正式id換位官方給的測試廣告位id
mRewardedVideoAd!!.loadAd("ca-app-pub-3940256099942544/5224354917", AdRequest.Builder().build())
2.你不是測試裝置
解決方案:新增自己的測試裝置號,需要從log日誌裡面尋找,如下圖
程式碼貼出來:
mRewardedVideoAd!!.loadAd("ca-app-pub-3940256099942544/5224354917", AdRequest.Builder()
.addTestDevice("176998C7DFA2CC4F0FDC2C42DD42ACF8") // An example device ID ,this code you should get from your log info and replace it ,after soon you can run and for a test。
.build())
哈哈,註釋寫成英文了,不過相信你一定能看懂我這傻瓜式的英文註釋,哈哈