[轉]cocos2d-x添加廣告條(IOS and Android)


IOS下比較簡單,加入storekit,添加三行代碼就成功了(摘自 @冬天的林  新浪微博



2:Android--Admob

Android下陷阱較多發火

我按照網上的教程還是遇到了一些問題。

下面大多摘錄自http://www.pin5i.com/showtopic-admob-android-tutorial.html。比較特殊的地方用藍色標記了

 

(1) 首先,當然是需要注冊一個Admob的帳號。Admob 的主頁是:http://www.admob.com/ 。 當然,如果你對於瀏覽英文網頁還有些障礙的話,可以登錄中文網站:http://zhcn.admob.com/ 。如果網站的文字還是英文,你可以在網站主頁的右下角的“Language”處,選擇“中文(簡體)”。點擊進入注冊頁面后,有一些欄目需要填寫,不要太過疑慮,就像你注冊一個論壇一樣,隨便填下就好了。最關鍵的是保證填寫的email地址有效,另外就是填上姓名,選擇語言。帳戶類型我選擇的“不確定”,語言“中文(簡體)”~ 提交注冊申請之后,不久你就會收到用於確認並激活帳號的電子郵件,點擊激活鏈接,就可以了激活你的Admob帳號了~


(2)        第二步就是設置你的Android應用程序信息,並獲得Admob的插入代碼。登錄你的Admob帳號后,在主頁的左上方(Logo上面)點擊 “Marketplace(手機廣告市場)”,進入頁面后,在“Sites&Apps(站點和應用程序)”標簽下,點擊“Add Site/App”。選擇我們熟悉的圖標——" Android App ” 。這時會出現需要你填寫一個“詳細信息”,隨便填上一些信息。(不要太過在意現在填寫的東西,因為這些以后都是可以修改的)。比如“Android Package URL” 我到現在都還沒有填寫,描述之類的,想寫就寫點吧。填好詳細信息后,點擊“繼續”,就可以到AdMob Android SDK 的下載頁面了。下載這個SDK(當然,這個很重要)。


        The AdMob Android SDK includes:


        README: Get started with AdMob Android ads! 
        AdMob Jar file: Required for publishing ads. Follow the documentation in javadoc/index.html and drop the AdMob Jar file into your project. 
        Sample Projects: Examples of AdMob Android ads shown in the LunarLander application.


(3)       第三步獲取你的應用程序對應的Publisher ID。在下載頁面點擊"Go to Sites/Apps"就可以到你應用程序的管理界面了。這時你會發現在這個頁面醒目的位置會有一個叫你填寫詳細信息的提示:


          在我們發送任何有待收入之前,您需要填寫技術聯系詳細信息和付款首選項。


        我們暫時可以不用管它,因為錢是會存在我們的Admob的賬戶上的,等我們需要提現的時候,或者你想填的時候再填就可以了。在下面的列表中,選擇你的應用程序並進入。這個界面就是你的應用程序廣告的管理界面了,里面有比較多的功能,以后可以慢慢了解,現在我們只需要知道兩個東西,一個是發布者 ID(Publisher ID),一個是你程序的狀態。Publisher ID是一個15個字符的字符串,而你程序的狀態現在應該還是不活動(Inactive)。我們下面要做的就是怎么讓它變為Active。


(4)      第四步代碼編寫——在你的應用程序中插入Admob廣告。經過上面的步驟,我們在網站上的設置就告一個段落了,現在我們終於要進入主題了,如何在自己的Android應用程序中插入Admob廣告。如果你不健忘的話,一定還記得我們之前下載的那個AdMob Android SDK 。解壓它,看看里面有些什么東西。這里面最重要的就是那個名為“admob-sdk-android.jar”的包啦,Admob將如何把廣告加載到 Android應用程序中的代碼集成在這個包里,我們編寫程序的時候就需要將這個包導入到我們的工程里面去。另外,解壓出來的文件夾中還有一個名為 “javadoc”的文件夾,打開它里面的index.html。它是關於Admob Android SDK的幫助文檔,在Package 下的Setup下,有詳細完整的在自己的應用程序中插入廣告的方法介紹,在這里我就偷懶,引用一下~


        Including the Jar


        Add the Jar file included with the SDK to your Android project as an external library. In your project's root directory create a subdirectory libs (this will already be done for you if you used Android's activitycreator). Copy the AdMob Jar file into that directory. For Eclipse projects:


        Go to the Properties of your project (right-click on your project from the Package Explorer tab and select Properties) 
        Select "Java Build Path" from left panel 
        Select "Libraries" tab from the main window 
        Click on "Add JARs..." 
        Select the JAR copied to the libs directory 
        Click "OK" to add the SDK to your android project


        注意:需要首先在你工程的根目錄下新建一個叫做“libs”的文件夾,並把之前所說的最重要的東西“admob-sdk- android.jar”復制到里面。

 

(5)修改AndroidManifest.xml
        Your AdMob publisher ID was given to you when creating your publisher account on www.admob.com before downloading this code. It is a 15-character code like a1496ced2842262. Just before the closing </application> tag add a line to set your publisher ID:

[html]  view plain copy
  1. <meta-data android:value="a14eafbb0936d03" android:name="ADMOB_PUBLISHER_ID" />  

 

必出需要修改value值為admob上顯示的用戶ID

Set any permissions not already included just before the closing </manifest> tag:

 

[html]  view plain copy
  1. <uses-permission android:name="android.permission.INTERNET"></uses-permission>  
  2. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>  

 

Setting ACCESS_COARSE_LOCATION (and/or ACCESS_FINE_LOCATION) allows narrowly geo-targeted ads be shown.


 

添加AdActivity屬性

[html]  view plain copy
  1. <activity android:name="com.google.ads.AdActivity"  
  2.                 android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>  

此處需要注意了,我在這兒折騰了很久,報的錯誤就是screenSize|smallestScreenSize 三個屬性不支持,如果將其刪除,則在Admob上顯示 "You must have AdActivity declared in AndroidManifest.xml with configChanges."

 

 

追究其原因,就是因為這三個屬性是在最新的SDK(>=13)上支持的,而我的project屬性中設置的是Android2.2,解決方法如下:

a:升級ADT,下載最新的SDK

b:修改Project Build Target的API Level,其值必須>=13,我選擇的是Android 4.0。修改步驟:右鍵點擊項目,選擇Properties-->Android-->Android 4.0

c:修改AndroidManifest.xml中的targetSdkVersion和minSdkVersion值為自己需要的值

 

[html]  view plain copy
  1. <uses-sdk android:targetSdkVersion="8" android:minSdkVersion="7"/>  

(6) 添加attrs.xml

 

The attrs.xml file specifies custom AdView attributes in XML layout files. If your application does not already have an /res/values/attrs.xml file then create one and copy-and-paste the following into it. If you do have that file then just add the declare-styleable element:

 

[html]  view plain copy
  1.         <?xml version="1.0" encoding="utf-8"?>  
  2.         <resources>  
  3.                 <declare-styleable name="com.admob.android.ads.AdView">  
  4.                         <attr name="testing" format="boolean" />  
  5.                         <attr name="backgroundColor" format="color" />  
  6.                         <attr name="textColor" format="color" />  
  7.                         <attr name="keywords" format="string" />  
  8.                         <attr name="refreshInterval" format="integer" />  
  9.                         <attr name="isGoneWithoutAd" format="boolean" />  
  10.                 </declare-styleable>  
  11.         </resources>  

7:顯示

下面兩種方法選擇一種即可

(7.1)添加顯示代碼(一)

這是寫代碼添加

 

[java]  view plain copy
  1. import com.google.ads.*;  
  2.   
  3. ........  
  4.     protected void onCreate(Bundle savedInstanceState){  
  5.         .......  
  6.        setupAds();  
  7.     }  
  8.    
  9.     private void setupAds()  
  10.     {  
  11.         LinearLayout layout = new LinearLayout(this);  
  12.         layout.setOrientation(LinearLayout.VERTICAL);  
  13.         addContentView(layout, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));  
  14.         AdView adView = new AdView(this, AdSize.BANNER, "a14eafbb0936d03");  
  15.         layout.addView(adView);  
  16.         adView.loadAd(new AdRequest());  
  17.     }     


(7.2)xml文件修改

 

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     android:orientation="vertical">  
  7.   
  8.     <EditText android:id="@+id/textField"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_width="fill_parent"  
  11.         android:background="@null"/>  
  12.   
  13.   
  14.     <org.cocos2dx.lib.Cocos2dxGLSurfaceView  
  15.         android:id="@+id/game_gl_surfaceview"  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="fill_parent"/>  
  18.   
  19.   
  20.     <RelativeLayout   
  21.                 android:id="@+id/ADLayout"  
  22.                 android:layout_width="wrap_content"  
  23.                 android:layout_height="wrap_content">  
  24.           
  25.   
  26.             <com.google.ads.AdView   
  27.                         android:id="@+id/adView"  
  28.                         android:layout_width="wrap_content"  
  29.                         android:layout_height="wrap_content"  
  30.                         android:layout_alignParentRight="true"  
  31.                         android:layout_alignParentBottom="true"  
  32.                         ads:adUnitId="a14eafbb0936d03"  
  33.                         ads:adSize="BANNER"  
  34.                         ads:loadAdOnCreate="true"/>  
  35.         </RelativeLayout>  
  36.       
  37. </FrameLayout>  


 


簡單來說就是在onCreate中調用setupAds即可,注意修改用戶ID ^_^

 


好了,一般來說通過上述步驟后,編譯運行就可以顯示廣告窗口了



BADA

請見:http://blog.csdn.net/dragoncheng/article/details/7070291


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM