Android Interstitial Ads 2.0
跳至導覽
跳至搜尋
簡介
- TWMInterstitialAd為全螢幕大小之廣告, 使用方法跟TWMAdView非常類似, 顯示廣告時需在onReceivedAd()之後呼叫show()函式以便顯示
注意:目前Interstitial廣告有相同版位ID在同一台裝置上,預設為120分鐘展示一次,如需調整請洽服務窗口
實作方式
- 廣告初始設置
TWMInterstitialAd twmInterstitial = new TWMInterstitialAd(this, "{YOUR_AD_UNIT_ID}");
- 加入TWMAdViewListener監聽廣告投放情況,並在onReceiveAd Callback內呼叫showAd()
- 在onReceiveAd後可透過showAd()顯示廣告
twmInterstitial.setAdListener(new TWMAdViewListener() {
@Override
public void onReceiveAd(TWMAd paramTWMAd) {
// 顯示 interstitial
if(paramTWMAd == twmInterstitial){
twmInterstitial.show();
}
}
@Override
public void onFailedToReceiveAd(TWMAd paramTWMAd, ErrorCode paramErrorCode) {}
@Override
public void onPresentScreen(TWMAd paramTWMAd) {}
@Override
public void onDismissScreen(TWMAd paramTWMAd) {}
@Override
public void onLeaveApplication(TWMAd paramTWMAd) {}
});
- 建立 TWMAdRequest
- 請求廣告前,你需要創建一個TWMAdRequest物件
TWMAdRequest request = new TWMAdRequest();
- 若額外設置使用者生日,可讓您的APP獲得更多元的廣告內容
| 使用Date | 使用Calendar |
|---|---|
TWMAdRequest request = new TWMAdRequest();
try {
//定義好時間字串的格式
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd", Locale.getDefault());
//將字串轉成Date型
Date birthDate = sdf.parse("1992/01/01");
// 將生日加入request
request.setBirthday(birthDate);
}catch (Exception e) {}
|
// 創建Calerdar並設定生日 (注意! 月份的編號是由0至11) Calendar birthDate = Calendar.getInstance(); // 設定生日為1992/01/01 birthDate.set(1992, 0, 1); // 將生日加入request TWMAdRequest request = new 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畫面展示
| 插頁式廣告呈現 |
|
