最近做了一個小的APP,並且嵌入了多盟廣告平台的廣告,以下小結一下如何嵌入多盟廣告:
我的新浪微博(wind瑞):http://weibo.com/tianrui1990
步驟一,下載多盟廣告SDK,將 domob_android_sdk.jar 匯入想要嵌入廣告的的工程中。
1. 右鍵您的工程根目錄,選擇“Properties”
2. 在左面板中選擇“Java Build Path”
3. 然後選擇“Libraries”標籤
4. 點擊“Add External JARs„”
5. 選擇
domob_android_sdk.jar的目錄路徑.
6. 點擊“OK”即匯入成功
步驟二,在AndroidManifest.xml檔案中配置使用者權限。
請務必配置以下許可權,否則將有可能擷取不到廣告。
1. android.permission.INTERNET,串連網路許可權 INTERNET ,用於請求廣告
2. android.permission.READ_PHONE_STATE,用於精確統計使用者手機的系統資訊
3. android.ACCESS_NETWORK_STATE,用於精確識別網路存取點等資訊
4. android.permission.ACCESS_COARSE_LOCATION,有助於精準投放地區廣告以及協助統計使用應用程式的使用者的地區分布情況
5. android.permission.WRITE_EXTERNAL_STORAGE,有助於實現圖片資源的緩衝,節省流量,並可獲得更好的使用者體驗
請將下面許可權配置代碼複製到 AndroidManifest.xml 檔案中:
<!-- 必須申明的許可權 --><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.READ_PHONE_STATE"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/><uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><!-- 以下為可選的許可權 --><!-- 使用GPS擷取使用者精確定位資訊 --><uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/><!-- 使用WIFI擷取使用者精確定位資訊 --><uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
步驟三,在AndroidManifest.xml中添加Activity聲明。
在 AndroidManifest.xml 檔案中的 </application> 標籤之前 ,還需要 添加 必要 的 Activity 聲明:
<activity android:name="cn.domob.android.ads.DomobActivity"
android:theme="@android:style/Theme.Translucent" />
步驟四,添加 Publisher ID 。
在 AndroidManifest.xml 檔案中的 </application> 標籤之前 添加PID聲明,假設你在多盟網站申請到的Publisher ID是999999#123,則需要在AndroidManifest.xml
檔案中加入如下一行:
<meta-data android:value="999999#123" android:name="DOMOB_PID" /><meta-data android:value="false" android:name="DOMOB_TEST_MODE"/>
注意:在<meta-data android:value="false" android:name="DOMOB_TEST_MODE"/>中,測試的時候設定android:value="true",正式發布或上傳時改為android:value="false"。
步驟五,使用xml布局嵌入廣告
1.在 res/values 檔案夾中添加 attrs.xml。如果你沒有添加這個檔案,那你將不能在 layout 中設定 顯示廣告的屬性。attrs.xml 檔案的內容如下:
<?xml version="1.0" encoding="UTF-8"?><resources><declare-styleable name="cn.domob.android.ads.DomobAdView"> <attr name="backgroundColor" format="color" /> <attr name="primaryTextColor" format="color" /> <attr name="keywords" format="string" /> <attr name="spots" format="string" /> <attr name="refreshInterval" format="integer" /> </declare-styleable></resources>
2.在布局main.xml中嵌入多盟廣告視圖:
以下為一個執行個體:
<?xml version="1.0" encoding="utf-8"?><!-- 需要設定命名空間 :umadsdk --><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:umadsdk="http://schemas.android.com/apk/res/com.youmi"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><cn.domob.android.ads.DomobAdView android:id="@+id/domobAd" android:layout_width="fill_parent" android:layout_height="wrap_content" myapp:keywords="game" myapp:refreshInterval="20" /> </LinearLayout>
注意: xmlns:umadsdk=”http://schemas.android.com/apk/res/您的應用程式套件名” 這句一定要加上,不然編輯器會提示錯誤。
我的新浪微博(wind瑞):http://weibo.com/tianrui1990
原作者:wind瑞
轉載請標明原作者和轉載地址,謝謝!
最後運行結果如所示:
我的新浪微博(wind瑞):http://weibo.com/tianrui1990