「Android Banner Ads 3.0 SDK8」修訂間的差異

出自TAMedia
跳至導覽 跳至搜尋
imported>Wikiuser
imported>Wikiuser
行 7: 行 7:
| align="center"|BANNER || align="center"|320x50 || align="center"|TWMAdSize.BANNER
| align="center"|BANNER || align="center"|320x50 || align="center"|TWMAdSize.BANNER
|-
|-
| align="center"|BANNER || align="center"|1200x627 || align="center"|TWMAdSize.BANNER
| align="center"|BANNER || align="center"|300x250 || align="center"|TWMAdSize.BANNER_300X250
|-
| align="center"|BANNER || align="center"|1200x627 || align="center"|TWMAdSize.BANNER_1200X627
|-
|-
| align="center"|SMART_BANNER || align="center"|自動對應裝置寬度, 並對應適當的高度 || align="center"|TWMAdSize.SMART_BANNER
| align="center"|SMART_BANNER || align="center"|自動對應裝置寬度, 並對應適當的高度 || align="center"|TWMAdSize.SMART_BANNER
|-
| align="center"|IAB_MRECT || align="center"|300x250 || align="center"|TWMAdSize.IAB_MRECT
|-
| align="center"|IAB_BANNER || align="center"|468x60 || align="center"|TWMAdSize.IAB_BANNER
|-
| align="center"|IAB_LEADERBOARD || align="center"|728x90 || align="center"|TWMAdSize.IAB_LEADERBOARD
|}
|}
<br><br>
<br><br>
行 139: 行 135:
   adView.loadAd(request);
   adView.loadAd(request);
<br><br>
<br><br>
*<span style="font-size:16px;">測試模式</span>
:如有需要,您可開啟測試模式來獲取測試廣告
  String deviceId = getDeviceId(getBaseContext());
  adView.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;
    }
  }


==<b>畫面展示</b>==
==<b>畫面展示</b>==

於 2021年8月27日 (五) 07:06 的修訂

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 = new TWMAdView(YourActivity.this, TWMAdSize.BANNER, TAMEDIA_BANNER_AD_UNIT_ID); 
 adView = TWMAdView(this,TWMAdSize.BANNER,TAMEDIA_BANNER_300x50)
 mRelativeLayout = findViewById<View>(R.id.ad_container) as RelativeLayout
 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) 
 root.addView(adView); 
您也可以將TWMAdView宣告於佈局XML中,在Java程式內透過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>
重要, 針對SDK 2.0後,請在Activity的生命週期中加入下述的呼叫,以便取得更好的服務經驗
 override fun onDestroy() {
       if (adView != null) {
           adView!!.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) {
       // native ad 並不使用本介面功能
    }
    override fun onDismissScreen(paramTWMAd: TWMAd) {
       // native ad 並不使用本介面功能
    }
    override fun onLeaveApplication(paramTWMAd: TWMAd) {
       // native ad 並不使用本介面功能
    }
 })



  • 建立 TWMAdRequest
請求廣告前,你需要創建一個TWMAdRequest物件
 adView?.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)



  • 請求廣告
請將TWMAdReqeust物件傳入loadAd
 adView.loadAd(request);	



畫面展示

應用程式開啟 點擊Banner後開啟網頁 點擊Banner後撥打電話
2.png 3.png 4.png



回首頁