Cocos2d-X 添加广告条



方法一:


freeket博客欢迎转载:http://blog.csdn.net/freeket

1: IOS--iAds

IOS下比较简单,加入storekit,添加三行代码就成功了(摘自 @冬天的林  新浪微博



2:Android--Admob

Androi下陷阱较多发火

我按照网上的教程还是遇到了一些问题。

下面大多摘录自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



方法二:http://www.2cto.com/kf/201406/312709.html


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值