「Android Banner Ads SDK8」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser |
imported>Wikiuser |
||
行 23: | 行 23: | ||
== <b>實作方式</b> == | == <b>實作方式</b> == | ||
*<span style="font-size:16px;">廣告初始設置</span> | *<span style="font-size:16px;">廣告初始設置</span> | ||
:動態生成TWMAdView加入Layout中 | |||
{| class="wikitable" | {| class="wikitable" | ||
行 57: | 行 58: | ||
|} | |} | ||
: | :或是將TWMAdView宣告至XML中, 透過findViewById取得TWMAdView | ||
<?xml version="1.0" encoding="utf-8"?> | <?xml version="1.0" encoding="utf-8"?> | ||
<LinearLayout xmlns:android=<nowiki>"http://schemas.android.com/apk/res/android"</nowiki> | <LinearLayout xmlns:android=<nowiki>"http://schemas.android.com/apk/res/android"</nowiki> | ||
行 73: | 行 74: | ||
</LinearLayout> | </LinearLayout> | ||
*<span style="font-size:16px;">加入TWMAdViewListener監聽廣告投放情況</span> | *<span style="font-size:16px;">加入TWMAdViewListener監聽廣告投放情況</span> | ||
adView.setAdListener(object : TWMAdViewListener { | adView.setAdListener(object : TWMAdViewListener { | ||
行 128: | 行 118: | ||
adView?.loadAd(adRequest) | adView?.loadAd(adRequest) | ||
| style="width:40%; padding: 1%;"| | | style="width:40%; padding: 1%;"| | ||
<b>// | <b>// 創建Calendar設定生日 (注意! 月份的編號是由0至11)</b> | ||
val birthDate = Calendar.getInstance() | val birthDate = Calendar.getInstance() | ||
<b>// 設定生日為1992/01/01</b> | <b>// 設定生日為1992/01/01</b> | ||
行 140: | 行 130: | ||
adView?.loadAd(adRequest) | adView?.loadAd(adRequest) | ||
|} | |} | ||
<br><br> | |||
:<b><span style="color:#ff0000">最後請記得, 於Activity.onDestroy()回收TWMAdView物件</span></b> | |||
<source> | |||
override fun onDestroy() { | |||
super.onDestroy() | |||
if (view is TWMAdView) { | |||
view.destroy() | |||
} | |||
mRelativeLayout?.removeAllViews() | |||
} | |||
</source> | |||
<br><br> | <br><br> | ||
行 155: | 行 158: | ||
<br><br> | <br><br> | ||
== <b>將應用程式的廣告請求標記為兒童導向內容(13 歲以下)</b> == | |||
<br> | |||
[http://wiki.tamedia.com.tw/androidDoc/library/com.taiwanmobile.pt.adp.view/-t-w-m-ad-request/index.html TWMAdRequest] | |||
<br> | |||
<br> | |||
:請於廣告請求時透過TWMAdRequest.setChildDirectedTreatment()告知SDK該廣告標記為兒童導向內容 | |||
:Android裝置的Google廣告ID (AAID) 於此模式時不會被存取與紀錄 | |||
<source> | |||
TWMAdView adView = TWMAdView(activity, adSize, adunitId) | |||
val paramAd = RelativeLayout.LayoutParams( | |||
RelativeLayout.LayoutParams.MATCH_PARENT, | |||
RelativeLayout.LayoutParams.WRAP_CONTENT | |||
) | |||
paramAd.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM) | |||
paramAd.addRule(RelativeLayout.CENTER_HORIZONTAL) | |||
adView?.layoutParams = paramAd | |||
adView?.visibility = View.INVISIBLE | |||
container?.addView(adView) | |||
adView?.loadAd(adRequest.apply { | |||
setChildDirectedTreatment(true) | |||
}) | |||
</source> | |||
[[Android SDK Developer Guide 2 | 回首頁]] | [[Android SDK Developer Guide 2 | 回首頁]] |
於 2021年11月17日 (三) 06:18 的修訂
Banner 廣告支援格式
- TAMedia Banner廣告支援下列數種形式:
廣告格式 | 大小(寬度x高度) | TWMAdSize 常數值 |
---|---|---|
BANNER | 320x50 | TWMAdSize.BANNER |
BANNER | 300x250 | TWMAdSize.BANNER_300X250 |
BANNER | 1200x627 | TWMAdSize.BANNER_1200X627 |
SMART_BANNER | 自動對應裝置寬度, 並對應適當的高度 | TWMAdSize.SMART_BANNER |
SDK設計文件
實作方式
- 廣告初始設置
- 動態生成TWMAdView加入Layout中
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) |
- 或是將TWMAdView宣告至XML中, 透過findViewById取得TWMAdView
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <com.taiwanmobile.pt.adp.view.TWMAdView xmlns:ad="http://schemas.android.com/apk/lib/com.taiwanmobile.pt.adp.view" android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" ad:adSize="BANNER" ad:adunitId="{AD_UNIT_ID}" > </com.taiwanmobile.pt.adp.view.TWMAdView> </LinearLayout>
- 加入TWMAdViewListener監聽廣告投放情況
adView.setAdListener(object : TWMAdViewListener { override fun onReceiveAd(ad: TWMAd) { // 表示本次請求收到廣告 } override fun onFailedToReceiveAd(ad: TWMAd, errorCode: TWMAdRequest.ErrorCode) { // 表示本次請求發生錯誤, 並沒有廣告內容可供使用 } override fun onPresentScreen(ad: TWMAd) { // 表示廣告內容已呈現 } override fun onDismissScreen(ad: TWMAd) { // 表示廣告內容已被關閉 } override fun onLeaveApplication(ad: TWMAd) { // 表示因點擊廣告或其他因素離開app } })
- 請求廣告
- 請求廣告前,你需要創建一個預設的TWMAdRequest物件
- 並將TWMAdReqeust物件傳入loadAd
adView?.loadAd(TWMAdRequest())
- 或是,透過TWMAdRequest額外設置使用者生日,可讓您的APP獲得更多元的廣告內容
使用Date | 使用Calendar |
---|---|
val request = TWMAdRequest() try { // 定義好時間字串的格式 val sdf = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()) // 將字串轉成Date型 val birthDate: Date = sdf.parse("1992/01/01") // 將生日加入request request.setBirthday(birthDate) } catch (Exception e) {} // 透過TWMAdRequest載入廣告 adView?.loadAd(adRequest) |
// 創建Calendar設定生日 (注意! 月份的編號是由0至11) val birthDate = Calendar.getInstance() // 設定生日為1992/01/01 birthDate[1992, 1] = 1 // 將生日加入request val adRequest = TWMAdRequest() request.setBirthday(birthDate) // 透過TWMAdRequest載入廣告 adView?.loadAd(adRequest) |
- 最後請記得, 於Activity.onDestroy()回收TWMAdView物件
override fun onDestroy() {
super.onDestroy()
if (view is TWMAdView) {
view.destroy()
}
mRelativeLayout?.removeAllViews()
}
畫面展示
應用程式開啟 | 點擊Banner後開啟網頁 | 點擊Banner後撥打電話 |
將應用程式的廣告請求標記為兒童導向內容(13 歲以下)
- 請於廣告請求時透過TWMAdRequest.setChildDirectedTreatment()告知SDK該廣告標記為兒童導向內容
- Android裝置的Google廣告ID (AAID) 於此模式時不會被存取與紀錄
TWMAdView adView = TWMAdView(activity, adSize, adunitId)
val paramAd = RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
)
paramAd.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)
paramAd.addRule(RelativeLayout.CENTER_HORIZONTAL)
adView?.layoutParams = paramAd
adView?.visibility = View.INVISIBLE
container?.addView(adView)
adView?.loadAd(adRequest.apply {
setChildDirectedTreatment(true)
})