x 3.0 final 如何在android里添加广告

1,,使用的是谷歌的AdMob的广告

先去注册个账号吧

怎么注册就不说了,注册需要翻墙,如何翻墙,自己解决,注册太长了,就那么回事,不说了。注册好后,登陆界面如下

按顺序点击,填写好相关内容后,你会得到这个界面

为什么是中文,google自动帮我翻译了,有时候翻译有时候不翻译,这里就不说了,看到广告单元ID没,把他记下来,

2,现在,新建一个的cocos2d-X 3.0的工程,你环境是不是按照上一篇的环境搭建篇搭建的呢,不是的话还不快去

茯苓新admobTest-P com.test.admobTest-L CPP-D D:\工作

先用日食导入工程,你看的很清楚,不是对,是Eclispe的

1)先导入libcocos2dx库

有关Eclispe中,文件 – >导入 – >现有的Android代码放入工作区

然后点击浏览…找到如下图路径

然后导入,库导入完了再导入admobTest工程

还是按照刚才的方法,不过赵路径的时候是下面这个

当你导入以后,工程会自动运行“的Python build_native.py”,如下图

你就直接连接手机运行就OK了,

3,下面,开始添加广告,

1)关闭Eclispe的,

2)打开翻墙软件,

3)打开SDK 的manager.exe选择工具- >选项…,如下配置

然后,关闭SDK的manager.exe重新打开,更新以下项目

更新完毕后,将下图中的文件夹

复制到你的工程目录下

打开Eclipse中,按照导入库方式导入谷歌播放services_lib

导入后,右键点击admobTest工程,选择属性属性,然后如图设置

将谷歌 – 播放services_lib加到工程中

修改的AndroidManifest.xml文件,如图

文本在此

<meta-data android:name="com.google.android.gms.version"android:value="@integer/google_play_services_version"/><activity android:name="com.google.android.gms.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

打开工程中的AppActivity.java文件,修改如下

/****************************************************************************Copyright (c) 2008-2010 Ricardo QuesadaCopyright (c) 2010-2012 cocos2d-x.orgCopyright (c) 2011Zynga Inc.Copyright (c) 2013-2014 Chukong Technologies Inc. Permission is hereby granted, free of charge, to any person obtaining a copyof this software and associated documentation files (the "Software"), to dealin the Software without restriction, including without limitation the rightsto use, copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom the Software isfurnished to do so, subject to the following conditions:The above copyright notice and this permission notice shall be included inall copies or substantial portions of the Software.THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS ORIMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THEAUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHERLIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS INTHE SOFTWARE. ****************************************************************************/package org.cocos2dx.cpp;import java.lang.reflect.InvocationTargetException;import java.lang.reflect.Method;import org.cocos2dx.lib.Cocos2dxActivity;import android.annotation.TargetApi;import android.graphics.Color;import android.graphics.Point;import android.os.Build;import android.os.Bundle;import android.view.Display;import android.view.View;import android.view.WindowManager;import android.widget.LinearLayout;import android.widget.RelativeLayout;import com.google.android.gms.ads.AdSize;import com.google.android.gms.ads.AdRequest;import com.google.android.gms.ads.AdView;public class AppActivity extends Cocos2dxActivity {private static AppActivity _appActiviy;private AdView adView;private static final String AD_UNIT_ID = "ca-app-pub-4321574338161624/7981547992";//填写你的广告单元ID// Helper get display screen to avoid deprecated function useprivate Point getDisplaySize(Display d) {if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {return getDisplaySizeGE11(d);}return getDisplaySizeLT11(d);}@TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)private Point getDisplaySizeGE11(Display d) {Point p = new Point(0, 0);d.getSize(p);return p;}private Point getDisplaySizeLT11(Display d) {try {Method getWidth = Display.class.getMethod("getWidth",new Class[] {});Method getHeight = Display.class.getMethod("getHeight",new Class[] {});return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(),((Integer) getHeight.invoke(d, (Object[]) null)).intValue());} catch (NoSuchMethodException e2) // None of these exceptions should// ever occur.{return new Point(-1, -1);} catch (IllegalArgumentException e2) {return new Point(-2, -2);} catch (IllegalAccessException e2) {return new Point(-3, -3);} catch (InvocationTargetException e2) {return new Point(-4, -4);}}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);//获取显示宽度int width = getDisplaySize(getWindowManager().getDefaultDisplay()).x;//顶部显示1,如果想在顶部显示广告请将顶部显示1和顶部显示2注释去掉,然后注释掉底部显示1,底部显示2,底部显示3//LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(//width, LinearLayout.LayoutParams.WRAP_CONTENT);//底部显示1RelativeLayout relativeLayout = new RelativeLayout(this);//this.frameLayout.addView(relativeLayout);//3.0 中,先打开Cocos2dxActivity.java文件,在52行,就是private static Context sContext = null;之后添加public static FrameLayout framelayout;然后在public void init() 函数中,将FrameLayout framelayout = new FrameLayout(this);改成framelayout = new FrameLayout(this);就可以了使用这句代码了,3.1.1之后什么都不要动,直接写成this.mFrameLayout.add(relativeLayout)就可以了//底部显示2RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(width,AdView.LayoutParams.WRAP_CONTENT);//adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);////广告adView = new AdView(this);adView.setAdSize(AdSize.BANNER);adView.setAdUnitId(AD_UNIT_ID);AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("HASH_DEVICE_ID").build();//底部显示3relativeLayout.addView(adView,adParams);////广告请求adView.loadAd(adRequest);adView.setBackgroundColor(Color.BLACK);adView.setBackgroundColor(0);//顶部显示2//addContentView(adView, adParams);//赋值_appActiviy = this;}public static void hideAd() {_appActiviy.runOnUiThread(new Runnable() {@Overridepublic void run() {if (_appActiviy.adView.isEnabled())_appActiviy.adView.setEnabled(false);if (_appActiviy.adView.getVisibility() != 4)_appActiviy.adView.setVisibility(View.INVISIBLE);}});}public static void showAd() {_appActiviy.runOnUiThread(new Runnable() {@Overridepublic void run() {if (!_appActiviy.adView.isEnabled())_appActiviy.adView.setEnabled(true);if (_appActiviy.adView.getVisibility() == 4)_appActiviy.adView.setVisibility(View.VISIBLE);}});}@Overrideprotected void onResume() {super.onResume();if (adView != null) {adView.resume();}}@Overrideprotected void onPause() {if (adView != null) {adView.pause();}super.onPause();}@Overrideprotected void onDestroy() {adView.destroy();super.onDestroy();}}记住要改成你的广告标识

JAVA的完成了,下面就是C + +的了

穷则思变,差则思勤!没有比人更高的山没有比脚更长的路。

x 3.0 final 如何在android里添加广告

相关文章:

你感兴趣的文章:

标签云: