「Android Native Ads SDK8」修訂間的差異
跳至導覽
跳至搜尋
imported>Wikiuser (新頁面: 回Android首頁<br> = SDK8 版本更新說明 = 原生廣告改用 UIView 的方式呈現,因此可自由設計排版,該 UIView 必須為 TADN...) |
imported>Wikiuser |
||
行 1: | 行 1: | ||
:您也可以將TWMAdView宣告於佈局XML中,在Kotlin程式內透過findViewById取得TWMAdView | |||
<androidx.constraintlayout.widget.ConstraintLayout | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content"> | |||
<!-- 廣告 --> | |||
<com.taiwanmobile.pt.adp.view.TWMNativeAdView | |||
android:id="@+id/nativeAdView" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
app:layout_constraintTop_toTopOf="parent" | |||
app:layout_constraintLeft_toLeftOf="parent" | |||
> | |||
<LinearLayout | |||
android:id="@+id/container" | |||
android:layout_width="match_parent" | |||
android:layout_height="wrap_content" | |||
android:layout_gravity="center" | |||
android:minHeight="50dp" | |||
android:orientation="vertical" | |||
android:paddingLeft="20dp" | |||
android:paddingRight="20dp"> | |||
<ImageView | |||
android:id="@+id/iv_icon_square" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:layout_marginTop="10dp" | |||
android:contentDescription="@string/test" /> | |||
<ImageView | |||
android:id="@+id/iv_icon_rect" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:contentDescription="@string/test" | |||
android:layout_marginTop="10dp" /> | |||
<TextView | |||
android:id="@+id/tv_short_subject" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:layout_marginTop="10dp" /> | |||
<TextView | |||
android:id="@+id/tv_long_subject" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:layout_marginTop="10dp" | |||
tools:text="long subject" /> | |||
<TextView | |||
android:id="@+id/tv_body" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:layout_marginTop="10dp" | |||
tools:text="body" /> | |||
<com.taiwanmobile.pt.adp.nativead.TWMMediaView | |||
android:id="@+id/ad_media" | |||
android:layout_width="300dp" | |||
android:layout_height="200dp" | |||
android:layout_marginTop="10dp" /> | |||
<TextView | |||
android:id="@+id/tv_call_to_action" | |||
android:layout_width="wrap_content" | |||
android:layout_height="wrap_content" | |||
android:padding="5dp" | |||
android:textSize="20sp" | |||
android:textColor="@color/cardview_light_background" | |||
/> | |||
</LinearLayout> | |||
</com.taiwanmobile.pt.adp.view.TWMNativeAdView> | |||
</androidx.constraintlayout.widget.ConstraintLayout> | |||
:*宣告原生性廣告版位 | |||
TWMNativeAd以簡單且單純的方式, 提供廣告內容供開發商使用,可在收到onReceivedAd()呼叫nativeAdContent()取得原生廣告內容, 並解析 | |||
<source lang="java" collapse="false" title="Native Ad"> | |||
// 第一個參數為Activity or Context, 第二個參數請輸入您的版位ID | |||
var twmNativeAd: TWMNativeAd = TWMNativeAd(this, AD_UNIT_ID); | |||
twmNativeAd.setAdListener(object : TWMAdViewListener { | |||
override fun onReceiveAd(paramTWMAd: TWMAd) { | |||
// 表示本次請求收到廣告, | |||
// 呼叫getNativeAdContent()取得廣告內容 | |||
val adContent: JSONObject = twmNativeAd.getNativeAdContent() | |||
} | |||
override fun onFailedToReceiveAd(paramTWMAd: TWMAd?, paramErrorCode: ErrorCode?) { | |||
// 表示本次請求發生錯誤, 並沒有廣告內容可供使用 | |||
} | |||
override fun onPresentScreen(paramTWMAd: TWMAd) { | |||
// native ad 並不使用本介面功能 | |||
} | |||
override fun onDismissScreen(paramTWMAd: TWMAd) { | |||
// native ad 並不使用本介面功能 | |||
} | |||
override fun onLeaveApplication(paramTWMAd: TWMAd) { | |||
// native ad 並不使用本介面功能 | |||
} | |||
}) | |||
// 透過TWMAdRequest載入廣告 | |||
twmNativeAd.loadAd(TWMAdRequest()) | |||
</source> | |||
(Optional) 在載入廣告前,可以透過TWMAdRequest設置使用者生日,讓您的APP獲得更多元的廣告內容<br> | |||
{| class="wikitable" | {| class="wikitable" | ||
|- | |- | ||
! 使用Date !! 使用Calendar | |||
|- | |- | ||
| style="width:60%; padding:1%;"| | |||
val adRequest = TWMAdRequest() | |||
try { | |||
<b>//定義好時間字串的格式</b> | |||
val sdf = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()) | |||
<b>//將字串轉成Date型</b> | |||
val birthDate: Date = sdf.parse("1992/01/01") | |||
<b>// 將生日加入request</b> | |||
request.setBirthday(birthDate) | |||
}catch (e: Exception) {} | |||
<b>// 透過TWMAdRequest載入廣告</b> | |||
twmNativeAd.loadAd(request) | |||
| style="width:40%; padding: 1%;"| | |||
<b>// 創建Calerdar並設定生日 (注意! 月份的編號是由0至11)</b> | |||
val birthDate = Calendar.getInstance() | |||
<b>// 設定生日為1992/01/01</b> | |||
birthDate[1992, 0] = 1 | |||
<b>// 將生日加入request</b> | |||
val request = TWMAdRequest() | |||
request.setBirthday(birthDate) | |||
<b>// 透過TWMAdRequest載入廣告</b> | |||
twmNativeAd.loadAd(request) | |||
|} | |} | ||
onReceiveAd(載入廣告完成)後,透過nativeAdContent取得的內容如下 : | |||
<source> | |||
/* | |||
"LONGSUBJECT": "長標題 long subject ,※預設值為空字串", | |||
"SHORTSUBJECT": "短標題 short subject ,※預設值為空字串", | |||
"BODY": "內文 body ,※預設值為空字串", | |||
"ICONSQUARE": "小圖示 方 icon square ,※預設值為空字串", | |||
"ICONRECTANGLE": "小圖示 長 icon rectangle,※預設值為空字串", | |||
"VIDEO": "影片 mediaContent,※預設值為空字串", | |||
"IMAGE960X640": "大圖 960x640,※預設值為空字串", | |||
"IMAGE1200X627": "大圖 1200x627,※預設值為空字串" | |||
*/ | |||
= | private var twmNativeAd: TWMNativeAd? = null | ||
override fun onReceiveAd(ad: TWMAd) { | |||
= | val nAd: TWMNativeAdContent? = twmNativeAd?.nativeAdContent | ||
= | nAd?.longSubject.let { longSubject -> | ||
mTextViewLongTitle?.text = longSubject | |||
mNativeView?.setLongSubjectView(mTextViewLongTitle) | |||
} | |||
nAd?.shortSubject.let { ShortSubject -> | |||
mTextViewShortTitle?.text = ShortSubject | |||
mNativeView?.setLongSubjectView(mTextViewShortTitle) | |||
} | |||
= | nAd?.body.let { body -> | ||
mTextViewContent?.text = body | |||
mNativeView?.setBodyView(mTextViewContent) | |||
} | |||
nAd?.iconSquare?.getDrawable().let{ squareDrawable -> | |||
.. | mImageView120x120?.setImageDrawable(squareDrawable) | ||
mNativeView?.setSquareIconView(mImageView120x120) | |||
} | } | ||
nAd?.iconRect?.getDrawable().let{ rectDrawable -> | |||
mImageView96x64?.setImageDrawable(rectDrawable) | |||
mNativeView?.setRectangleIconView(mImageView96x64) | |||
} | } | ||
nAd?.callToAction.let{ mCTA -> | |||
mTextViewCTA?.text = mCTA | |||
mNativeView?.setCallToActionView(mTextViewCTA) | |||
} | } | ||
nAd?.mediaContent?.let{ content -> | |||
when(mAdMediaPreferImageSizeBig) { | |||
.. | true -> {content.setMainImageSize(TWMMediaContent.ImageSize.BIG)} | ||
false -> {content.setMainImageSize(TWMMediaContent.ImageSize.SMALL)} | |||
} | } | ||
mVideo?.setMediaContent(content) | |||
mNativeView?.setMediaView(mVideo) | |||
} | } | ||
mNativeView?.setNativeAd(twmNativeAd) | |||
} | } | ||
</source> | </source> | ||
= | <b><span style="color:#ff0000">(SDK5以上) 請在Activity的生命週期中加入下述的呼叫, 以正確release twmNativeAd</span></b> | ||
override fun onDestroy() { | |||
< | twmNativeAd?.destroy() | ||
super.onDestroy() | |||
} | |||
== | 原生性廣告內容與廣告版位關係表 | ||
<br> | |||
< | <b><span style="color:#ff0000">※重要, 下表所描述的廣告素材有可能因廣告主未提供素材, 而以空字串提供, 使用上請特別留意</span></b> | ||
{| width="100%" border="1" | |||
|- | |||
| align="center" | 素材 \ 版位 | |||
| align="center" | 限制條件 | |||
| align="center" | 條列式 | |||
| align="center" | 圖片式 (橫 16:9) | |||
| align="center" | 圖片式 (橫 3:2) | |||
| align="center" | 小圖式 | |||
| align="center" | 影音式 | |||
|- | |||
| align="center" | 長標題<br/>(LONGSUBJECT) | |||
| align="center" | 25字內 | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | | |||
| align="center" | V | |||
|- | |||
| align="center" | 短標題<br/>(SHORTSUBJECT) | |||
| align="center" | 10字內 | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | | |||
|- | |||
| align="center" | 內文<br/>(BODY) | |||
| align="center" | 70字內 | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | | |||
| align="center" | V | |||
|- | |||
| align="center" | 小圖 (方)<br/>(ICONSQUARE) | |||
| align="center" | 120x120<br/>jpg/png | |||
| align="center" | V | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | V | |||
| align="center" | V | |||
|- | |||
| align="center" | 小圖 (長)<br/>(ICONRECTANGLE) | |||
| align="center" | 96x64<br/>jpg/png | |||
| align="center" | V | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | V | |||
|- | |||
| align="center" | 影片<br/>(VIDEO) | |||
| align="center" | MP4, 10mb | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | V | |||
|- | |||
| align="center" | 大圖 (橫 3:2)<br/>(IMAGE960X640) | |||
| align="center" | 960x640<br/>jpg/png | |||
| align="center" | | |||
| align="center" | | |||
| align="center" | V | |||
| align="center" | | |||
| align="center" | V | |||
|- | |||
| align="center" | 大圖<br/>(IMAGE1200X627) | |||
| align="center" | 1200x627<br/>jpg/png | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
| align="center" | V | |||
|} | |||
< | <br><br> | ||
[[Android SDK Developer Guide 2 | 回首頁]] | |||
< |
於 2021年8月26日 (四) 11:23 的修訂
- 您也可以將TWMAdView宣告於佈局XML中,在Kotlin程式內透過findViewById取得TWMAdView
<androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="wrap_content"> <com.taiwanmobile.pt.adp.view.TWMNativeAdView android:id="@+id/nativeAdView" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_constraintTop_toTopOf="parent" app:layout_constraintLeft_toLeftOf="parent" > <LinearLayout android:id="@+id/container" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:minHeight="50dp" android:orientation="vertical" android:paddingLeft="20dp" android:paddingRight="20dp"> <ImageView android:id="@+id/iv_icon_square" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:contentDescription="@string/test" /> <ImageView android:id="@+id/iv_icon_rect" android:layout_width="wrap_content" android:layout_height="wrap_content" android:contentDescription="@string/test" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tv_short_subject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tv_long_subject" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" tools:text="long subject" /> <TextView android:id="@+id/tv_body" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" tools:text="body" /> <com.taiwanmobile.pt.adp.nativead.TWMMediaView android:id="@+id/ad_media" android:layout_width="300dp" android:layout_height="200dp" android:layout_marginTop="10dp" /> <TextView android:id="@+id/tv_call_to_action" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="5dp" android:textSize="20sp" android:textColor="@color/cardview_light_background" /> </LinearLayout> </com.taiwanmobile.pt.adp.view.TWMNativeAdView> </androidx.constraintlayout.widget.ConstraintLayout>
- 宣告原生性廣告版位
TWMNativeAd以簡單且單純的方式, 提供廣告內容供開發商使用,可在收到onReceivedAd()呼叫nativeAdContent()取得原生廣告內容, 並解析
// 第一個參數為Activity or Context, 第二個參數請輸入您的版位ID
var twmNativeAd: TWMNativeAd = TWMNativeAd(this, AD_UNIT_ID);
twmNativeAd.setAdListener(object : TWMAdViewListener {
override fun onReceiveAd(paramTWMAd: TWMAd) {
// 表示本次請求收到廣告,
// 呼叫getNativeAdContent()取得廣告內容
val adContent: JSONObject = twmNativeAd.getNativeAdContent()
}
override fun onFailedToReceiveAd(paramTWMAd: TWMAd?, paramErrorCode: ErrorCode?) {
// 表示本次請求發生錯誤, 並沒有廣告內容可供使用
}
override fun onPresentScreen(paramTWMAd: TWMAd) {
// native ad 並不使用本介面功能
}
override fun onDismissScreen(paramTWMAd: TWMAd) {
// native ad 並不使用本介面功能
}
override fun onLeaveApplication(paramTWMAd: TWMAd) {
// native ad 並不使用本介面功能
}
})
// 透過TWMAdRequest載入廣告
twmNativeAd.loadAd(TWMAdRequest())
(Optional) 在載入廣告前,可以透過TWMAdRequest設置使用者生日,讓您的APP獲得更多元的廣告內容
使用Date | 使用Calendar |
---|---|
val adRequest = TWMAdRequest() try { //定義好時間字串的格式 val sdf = SimpleDateFormat("yyyy/MM/dd", Locale.getDefault()) //將字串轉成Date型 val birthDate: Date = sdf.parse("1992/01/01") // 將生日加入request request.setBirthday(birthDate) }catch (e: Exception) {} // 透過TWMAdRequest載入廣告 twmNativeAd.loadAd(request) |
// 創建Calerdar並設定生日 (注意! 月份的編號是由0至11) val birthDate = Calendar.getInstance() // 設定生日為1992/01/01 birthDate[1992, 0] = 1 // 將生日加入request val request = TWMAdRequest() request.setBirthday(birthDate) // 透過TWMAdRequest載入廣告 twmNativeAd.loadAd(request) |
onReceiveAd(載入廣告完成)後,透過nativeAdContent取得的內容如下 :
/*
"LONGSUBJECT": "長標題 long subject ,※預設值為空字串",
"SHORTSUBJECT": "短標題 short subject ,※預設值為空字串",
"BODY": "內文 body ,※預設值為空字串",
"ICONSQUARE": "小圖示 方 icon square ,※預設值為空字串",
"ICONRECTANGLE": "小圖示 長 icon rectangle,※預設值為空字串",
"VIDEO": "影片 mediaContent,※預設值為空字串",
"IMAGE960X640": "大圖 960x640,※預設值為空字串",
"IMAGE1200X627": "大圖 1200x627,※預設值為空字串"
*/
private var twmNativeAd: TWMNativeAd? = null
override fun onReceiveAd(ad: TWMAd) {
val nAd: TWMNativeAdContent? = twmNativeAd?.nativeAdContent
nAd?.longSubject.let { longSubject ->
mTextViewLongTitle?.text = longSubject
mNativeView?.setLongSubjectView(mTextViewLongTitle)
}
nAd?.shortSubject.let { ShortSubject ->
mTextViewShortTitle?.text = ShortSubject
mNativeView?.setLongSubjectView(mTextViewShortTitle)
}
nAd?.body.let { body ->
mTextViewContent?.text = body
mNativeView?.setBodyView(mTextViewContent)
}
nAd?.iconSquare?.getDrawable().let{ squareDrawable ->
mImageView120x120?.setImageDrawable(squareDrawable)
mNativeView?.setSquareIconView(mImageView120x120)
}
nAd?.iconRect?.getDrawable().let{ rectDrawable ->
mImageView96x64?.setImageDrawable(rectDrawable)
mNativeView?.setRectangleIconView(mImageView96x64)
}
nAd?.callToAction.let{ mCTA ->
mTextViewCTA?.text = mCTA
mNativeView?.setCallToActionView(mTextViewCTA)
}
nAd?.mediaContent?.let{ content ->
when(mAdMediaPreferImageSizeBig) {
true -> {content.setMainImageSize(TWMMediaContent.ImageSize.BIG)}
false -> {content.setMainImageSize(TWMMediaContent.ImageSize.SMALL)}
}
mVideo?.setMediaContent(content)
mNativeView?.setMediaView(mVideo)
}
mNativeView?.setNativeAd(twmNativeAd)
}
(SDK5以上) 請在Activity的生命週期中加入下述的呼叫, 以正確release twmNativeAd
override fun onDestroy() { twmNativeAd?.destroy() super.onDestroy() }
原生性廣告內容與廣告版位關係表
※重要, 下表所描述的廣告素材有可能因廣告主未提供素材, 而以空字串提供, 使用上請特別留意
素材 \ 版位 | 限制條件 | 條列式 | 圖片式 (橫 16:9) | 圖片式 (橫 3:2) | 小圖式 | 影音式 |
長標題 (LONGSUBJECT) |
25字內 | V | V | V | V | |
短標題 (SHORTSUBJECT) |
10字內 | V | V | V | V | |
內文 (BODY) |
70字內 | V | V | V | V | |
小圖 (方) (ICONSQUARE) |
120x120 jpg/png |
V | V | V | ||
小圖 (長) (ICONRECTANGLE) |
96x64 jpg/png |
V | V | |||
影片 (VIDEO) |
MP4, 10mb | V | ||||
大圖 (橫 3:2) (IMAGE960X640) |
960x640 jpg/png |
V | V | |||
大圖 (IMAGE1200X627) |
1200x627 jpg/png |
V | V | V | V | V |