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帐号了~(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:
<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:
<uses-permission android:name="android.permission.INTERNET"></uses-permission> <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属性
<activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
追究其原因,就是因为这三个属性是在最新的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值为自己需要的值
<uses-sdk android:targetSdkVersion="8" android:minSdkVersion="7"/>
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:
<?xml version="1.0" encoding="utf-8"?> <resources> <declare-styleable name="com.admob.android.ads.AdView"> <attr name="testing" format="boolean" /> <attr name="backgroundColor" format="color" /> <attr name="textColor" format="color" /> <attr name="keywords" format="string" /> <attr name="refreshInterval" format="integer" /> <attr name="isGoneWithoutAd" format="boolean" /> </declare-styleable> </resources>
7:显示
下面两种方法选择一种即可
(7.1)添加显示代码(一)
这是写代码添加
import com.google.ads.*; ........ protected void onCreate(Bundle savedInstanceState){ ....... setupAds(); } private void setupAds() { LinearLayout layout = new LinearLayout(this); layout.setOrientation(LinearLayout.VERTICAL); addContentView(layout, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); AdView adView = new AdView(this, AdSize.BANNER, "a14eafbb0936d03"); layout.addView(adView); adView.loadAd(new AdRequest()); }
(7.2)XML方式
(7.2.1)res下layout的xml文件修改
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <EditText android:id="@+id/textField" android:layout_height="wrap_content" android:layout_width="fill_parent" android:background="@null"/> <org.cocos2dx.lib.Cocos2dxGLSurfaceView android:id="@+id/game_gl_surfaceview" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <RelativeLayout android:id="@+id/ADLayout" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.google.ads.AdView android:id="@+id/adView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_alignParentBottom="true" ads:adUnitId="a14eafbb0936d03" ads:adSize="BANNER" ads:loadAdOnCreate="true"/> </RelativeLayout> </FrameLayout>
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); initAds(); } public void initAds() { AdView ad = (AdView) findViewById(R.id.adView); ad.setAdListener(this); }
简单来说就是在onCreate中调用setupAds即可,注意修改用户ID ^_^
好了,一般来说通过上述步骤后,编译运行就可以显示广告窗口了
BADA
请见:http://blog.csdn.net/dragoncheng/article/details/7070291