在andengine中加入ADMOB广告

最近用Andengine写了个小游戏费了些周折。有了些心得现在把它写下来。

由于谷歌将不再接受ADMOB 6.4.1SDK及之前的版本,而推荐使用Google Play Sevices。 打开android sdk manager,下拉到底部如图下安装


将..\sdk\extras\google\google_play_services\libproject下的项目导入复制到你的workspace里。打开属性页面


如图所示添加。

接下来在AndroidManifest.xml添加权限

<!-- Used to request banner and interstitial ads. -->
 <uses-permission android:name="android.permission.INTERNET"/> 
<!-- Used to avoid sending an ad request if there is no connectivity. -->
 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
添加Activity

<!-- Activity required to show ad overlays. -->
 <activity android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

现在回游戏里加入以下代码

public class MainActivity extends BaseGameActivity {
	private AdView adView;
	private RelativeLayout bannerContainer;

@Override
protected void onSetContentView() {


		final FrameLayout frameLayout = new FrameLayout(this);

		// CREATING the layout parameters, fill the screen //

		final FrameLayout.LayoutParams frameLayoutLayoutParams =

		new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT,

		LayoutParams.MATCH_PARENT);

		this.adView = new AdView(this);
		adView.setAdSize( AdSize.BANNER);
		adView.setAdUnitId(Constants.AdmobId);

		adView.refreshDrawableState();
		adView.setVisibility(View.VISIBLE);//这里将广告设为不可视



		final FrameLayout.LayoutParams adViewLayoutParams =

		new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT,

		LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL
				| Gravity.BOTTOM);

	
		AdRequest adRequest=new AdRequest.Builder().addTestDevice("你设备ID")
				.build();
	
		adView.loadAd(adRequest);



		this.mRenderSurfaceView = new RenderSurfaceView(this);

		mRenderSurfaceView.setRenderer(mEngine, this);



		final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =

		new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());



		frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);

		frameLayout.addView(adView, adViewLayoutParams);
		


		this.setContentView(frameLayout, frameLayoutLayoutParams);

	}
我们不能让广告一直可见在需要的地方调用
public void setAdMobInVisibile() {
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adView.setVisibility(View.INVISIBLE);//设定广告为可视
			}
		});
	}
public void setAdMobVisibile() {
		this.runOnUiThread(new Runnable() {
			@Override
			public void run() {
				adView.setVisibility(View.VISIBLE);//设定广告为不可视可视


			}

		});
	}
到此就完成了。附上获取设备ID代码

String deviceId = Secure.getString(this.getContentResolver(),
                Secure.ANDROID_ID);
 Toast.makeText(this, deviceId, Toast.LENGTH_SHORT).show();

转载请附上原址http://blog.csdn.net/ldk1119/article/details/24839887

我的应用https://play.google.com/store/apps/details?id=org.andengine.movecube1


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值