「Android Banner Ads 3.0 SDK8」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser (→實作方式) |
||
行 39: | 行 39: | ||
<b>// 依序傳入Activity, TWMAdSize, 以及Ad Unit Id,TWMAdSize請參考頁首的TWMAdSize常數值</b> | <b>// 依序傳入Activity, TWMAdSize, 以及Ad Unit Id,TWMAdSize請參考頁首的TWMAdSize常數值</b> | ||
adView = | adView = TWMAdView(this,TWMAdSize.BANNER,TAMEDIA_BANNER_AD_UNIT_ID) | ||
val layoutParams = RelativeLayout.LayoutParams( | val layoutParams = RelativeLayout.LayoutParams( | ||
RelativeLayout.LayoutParams.WRAP_CONTENT, | |||
RelativeLayout.LayoutParams.WRAP_CONTENT | |||
) | |||
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) | layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) | ||
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL) | layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL) | ||
adView!!.layoutParams = layoutParams | adView!!.layoutParams = layoutParams | ||
mRelativeLayout?.addView(adView) | mRelativeLayout?.addView(adView) | ||
|} | |} | ||
:<b><span style="color:#ff0000">重要, 針對SDK 2.0後,請在Activity的生命週期中加入下述的呼叫,以便取得更好的服務經驗</span></b> | :<b><span style="color:#ff0000">重要, 針對SDK 2.0後,請在Activity的生命週期中加入下述的呼叫,以便取得更好的服務經驗</span></b> | ||
override fun onDestroy() { | override fun onDestroy() { | ||
if ( | if (view is TWMAdView) { | ||
view.destroy() | |||
} | } | ||
mRelativeLayout?.removeAllViews() | mRelativeLayout?.removeAllViews() | ||
行 89: | 行 70: | ||
} | } | ||
override fun onPresentScreen(paramTWMAd: TWMAd) { | override fun onPresentScreen(paramTWMAd: TWMAd) { | ||
// | // Banner ad 並不使用本介面功能 | ||
} | } | ||
override fun onDismissScreen(paramTWMAd: TWMAd) { | override fun onDismissScreen(paramTWMAd: TWMAd) { | ||
// | // Banner ad 並不使用本介面功能 | ||
} | } | ||
override fun onLeaveApplication(paramTWMAd: TWMAd) { | override fun onLeaveApplication(paramTWMAd: TWMAd) { | ||
// | // Banner ad 並不使用本介面功能 | ||
} | } | ||
}) | }) | ||
<br><br> | <br><br> | ||
*<span style="font-size:16px;"> | *<span style="font-size:16px;">請求廣告</span> | ||
:請求廣告前,你需要創建一個TWMAdRequest物件 | :請求廣告前,你需要創建一個TWMAdRequest物件 | ||
adView?.loadAd(TWMAdRequest()) | :並將TWMAdReqeust物件傳入loadAd | ||
adView?.loadAd(TWMAdRequest()) | |||
<br><br> | <br><br> | ||
於 2021年9月7日 (二) 08:02 的修訂
Banner SD8 廣告支援格式
- TAMedia Banner廣告支援下列數種形式:
廣告格式 | 大小(寬度x高度) | TWMAdSize 常數值 |
---|---|---|
BANNER | 320x50 | TWMAdSize.BANNER |
BANNER | 300x250 | TWMAdSize.BANNER_300X250 |
BANNER | 1200x627 | TWMAdSize.BANNER_1200X627 |
SMART_BANNER | 自動對應裝置寬度, 並對應適當的高度 | TWMAdSize.SMART_BANNER |
實作方式
- 廣告初始設置
Layout (XML範例) | Activity(Kotlin) |
---|---|
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android:id="@+id/ad_container" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal"> </RelativeLayout> |
var adView: TWMAdView? = null var mRelativeLayout: RelativeLayout? = null companion object { const val TAMEDIA_BANNER_AD_UNIT_ID: String = "INSERT_YOUR_AD_UNIT_ID" } // Other Implement ... // 依序傳入Activity, TWMAdSize, 以及Ad Unit Id,TWMAdSize請參考頁首的TWMAdSize常數值 adView = TWMAdView(this,TWMAdSize.BANNER,TAMEDIA_BANNER_AD_UNIT_ID) val layoutParams = RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT ) layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL) adView!!.layoutParams = layoutParams mRelativeLayout?.addView(adView) |
- 重要, 針對SDK 2.0後,請在Activity的生命週期中加入下述的呼叫,以便取得更好的服務經驗
override fun onDestroy() { if (view is TWMAdView) { view.destroy() } mRelativeLayout?.removeAllViews() super.onDestroy() }
- 加入TWMAdViewListener監聽廣告投放情況
adView.setAdListener(object : TWMAdViewListener { override fun onReceiveAd(paramTWMAd: TWMAd) { // 表示本次請求收到廣告, } override fun onFailedToReceiveAd(paramTWMAd: TWMAd?, paramErrorCode: ErrorCode?) { // 表示本次請求發生錯誤, 並沒有廣告內容可供使用 } override fun onPresentScreen(paramTWMAd: TWMAd) { // Banner ad 並不使用本介面功能 } override fun onDismissScreen(paramTWMAd: TWMAd) { // Banner ad 並不使用本介面功能 } override fun onLeaveApplication(paramTWMAd: TWMAd) { // Banner ad 並不使用本介面功能 } })
- 請求廣告
- 請求廣告前,你需要創建一個TWMAdRequest物件
- 並將TWMAdReqeust物件傳入loadAd
adView?.loadAd(TWMAdRequest())
畫面展示
應用程式開啟 | 點擊Banner後開啟網頁 | 點擊Banner後撥打電話 |