在Andengine里镶嵌广告

研究了下 加广告    

首先得去第三方那里下载对应的第三方包  我是用多盟的

xml布局

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.src.guang"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="4" />
   <!-- 注册所需的权限 -->
   <uses-permission android:name="android.permission.WAKE_LOCK"/>
   <uses-permission android:name="android.permission.INTERNET" />
	<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
	<uses-permission android:name="android.permission.READ_PHONE_STATE" />
	<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
	<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
	<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:label="@string/app_name"
            android:name=".GuangGaoActivity" 
             android:screenOrientation="landscape"  android:configChanges="keyboardHidden|orientation"
            >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <!-- 注册第三方包的activity -->
         <activity android:name="cn.domob.android.ads.DomobActivity"
			android:theme="@android:style/Theme.Translucent"></activity>
    </application>

</manifest>

 java代码

在onCreate方法里

public class GuangGaoActivity extends BaseGameActivity {
	/** Called when the activity is first created. */

	private Camera mCamera;
	private final int width = 800;
	private final int height = 480;
	private BitmapTextureAtlas textureAtlas;
	private TextureRegion region;
	private DomobAdView mAdview320x50;//初始化多盟广告第三包类
	@Override
	public Engine onLoadEngine() {
		mCamera = new Camera(0, 0, width, height);
		EngineOptions engineOptions = new EngineOptions(true,
				ScreenOrientation.LANDSCAPE, new RatioResolutionPolicy(width,
						height), mCamera);
		Engine engine = new Engine(engineOptions);

		return engine;
	}

	@Override
	public void onLoadResources() {
		textureAtlas = new BitmapTextureAtlas(1024, 512,
				TextureOptions.BILINEAR_PREMULTIPLYALPHA);
		region = BitmapTextureAtlasTextureRegionFactory.createFromAsset(
				textureAtlas, this, "gfx/meizi.jpg", 0, 0);
		getEngine().getTextureManager().loadTexture(textureAtlas);
	}

	@Override
	public Scene onLoadScene() {
		Scene scene = new Scene();

		scene.setBackground(new EntityBackground(new Sprite(0, 0, region)));
		return scene;
	}

	@Override
	public void onLoadComplete() {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onCreate(Bundle pSavedInstanceState) {

		super.onCreate(pSavedInstanceState);
        if(mAdview320x50==null){
			
			mAdview320x50 = new DomobAdView(this, "56OJyM1ouMGoaSnvCK",
					DomobAdView.INLINE_SIZE_320X50);
			mAdview320x50.setKeyword("game");
			mAdview320x50.setUserGender("male");
			mAdview320x50.setUserBirthdayStr("2000-08-08");
			mAdview320x50.setUserPostcode("123456");

			// 设置广告view的监听器。
			mAdview320x50.setAdEventListener(new DomobAdEventListener() {

				@Override
				public void onDomobAdReturned(DomobAdView adView) {
					// TODO Auto-generated method stub
					Log.i("DomobSDKDemo", "onReceivedFreshAd");

				}

				@Override
				public void onDomobAdFailed(DomobAdView adView) {
					// TODO Auto-generated method stub
					Log.i("DomobSDKDemo", "onReceivedFailed");

				}

				@Override
				public void onDomobAdOverlayPresented(DomobAdView adView) {
					// TODO Auto-generated method stub
					Log.i("DomobSDKDemo", "overlayPresented");
				}

				@Override
				public void onDomobAdOverlayDismissed(DomobAdView adView) {
					// TODO Auto-generated method stub
					Log.i("DomobSDKDemo", "Overrided be dismissed");

				}
			});
		//初始化相对布局
		RelativeLayout relativeLayout = new RelativeLayout(this);
		RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.WRAP_CONTENT,
				RelativeLayout.LayoutParams.WRAP_CONTENT);
		
		layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);//居中
		
		//把广告view加入布局中
		relativeLayout.addView(mAdview320x50,layoutParams);
		
		RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(
				RelativeLayout.LayoutParams.FILL_PARENT,
				RelativeLayout.LayoutParams.FILL_PARENT);
		
		addContentView(relativeLayout, layoutParams1);
		
		
        }
	}
}

  效果:

原文地址:https://www.cnblogs.com/wzachenjian/p/2741962.html