您的位置:首页 > 编程语言 > Go语言

在andengine中加入ADMOB广告

2014-05-01 14:15 387 查看
最近用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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息