Android Interstitial Ads 2.0 SDK8
於 2021年8月26日 (四) 12:46 由 imported>Wikiuser 所做的修訂
簡介
SDK8
- TWMInterstitialAd為全螢幕大小之廣告, 使用方法跟TWMAdView非常類似, 顯示廣告時需在onReceivedAd()之後呼叫show()函式以便顯示
注意:目前Interstitial廣告有相同版位ID在同一台裝置上,預設為120分鐘展示一次,如需調整請洽服務窗口
實作方式
- 廣告初始設置
var interstitialAd : TWMInterstitialAd? = null
TWMInterstitialAd twmInterstitial = new TWMInterstitialAd(this, "{YOUR_AD_UNIT_ID}");
interstitialAd = TWMInterstitialAd(this ,TAMEDIA_INTERSTITIAL_ADUNITID)
companion object{
const val TAMEDIA_INTERSTITIAL_ADUNITID: String = "{TAMEDIA_INTERSTITIAL_ADUNITID}"
}
- 加入TWMAdViewListener監聽廣告投放情況,並在onReceiveAd Callback內呼叫showAd()
- 在onReceiveAd後可透過showAd()顯示廣告
interstitialAd.setAdListener(object : TWMAdViewListener {
override fun onReceiveAd(paramTWMAd: TWMAd) {
// 表示本次請求收到廣告,
interstitialAd?.show()
}
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
- 請求廣告前,你需要創建一個TWMAdRequest物件
interstitialAd?.loadAd(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) {}
|
// 創建Calerdar並設定生日 (注意! 月份的編號是由0至11) val birthDate = Calendar.getInstance() // 設定生日為1992/01/01 birthDate[1992, 0] = 1 // 將生日加入request val request = TWMAdRequest() request.setBirthday(birthDate) |
- 請求廣告
twmInterstitial.loadAd(request);
- 若需要進行廣告投放的測試,可使用測試模式來請求廣告, ※測試模式將不會紀錄任何的曝光以及點擊數據
String deviceId = getDeviceId(getBaseContext()); twmInterstitial.loadAd(new TWMAdRequest().addTestDevice(deviceId));
private String getDeviceId(final Context context) {
String deviceId = null;
try {
deviceId = android.provider.Settings.Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
if (deviceId!= null) {
return convertToMD5ID(deviceId);
}
} catch (Exception e) {
Log.e("getDeviceId", e.getMessage(), e);
}
return deviceId;
}
private String convertToMD5ID(final String string) {
if (string == null) {
return null;
}
try {
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(string.getBytes());
BigInteger number = new BigInteger(1, md.digest());
String md5 = number.toString(16);
while (md5.length() < 32)
md5 = "0" + md5;
return md5;
} catch (NoSuchAlgorithmException e) {
return null;
}
}
Interstitial畫面展示
| 插頁式廣告呈現 |
|
