Displaying Ad In AndEngine

2019-02-09 16:43发布

问题:

I am trying to display an advertisement using Greystrip in AndEngine.

I cannot figure out how this is done because it doesnt use a Layout to inflate views, but yet sprites.

i use BaseGameActivity to create my application for each scene i would like to display adds on.

In GreyStrip this is how they tell you to integrate ads in your application..

Before adding calls in your application to GSSDK, you need to incorporate the SDK into your AndroidManifest.xml. Add the following in the section, replacing with a package identifier that is unique to your application. This Content Provider manages local storage of ad content, while the Activity manages ad display.

 <provider android:name="com.greystripe.android.sdk.AdContentProvider"
    android:authorities="<YOUR_APPLICATION_PACKAGE>.AdContentProvider"
android:multiprocess="true"
android:exported="false" />
<activity android:name="com.greystripe.android.sdk.AdView"
android:configChanges="keyboard|keyboardHidden|orientation" >
<intent-filter>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

To initialize the Greystripe SDK, call the initialize method at startup. This should be done within your application’s onCreate() method. This call will spawn a background thread to initialize our activity, and then return control to your app. In this background, the Greystripe activity will download ads as well as any SDK updates. Parameters: ctx: Your application Context instance appId: Use the appId provided during app registration. Providing an invalid appId will cause the SDK to display error notification ads.

 public static GSSDK initialize(Context ctx, String appId)

To use a banner, place the following in your main.xml file:

<view class="com.greystripe.android.sdk.BannerView"
android:id="@+id/gsBanner"
android:layout_width="320dp"
android:layout_height="48dp"/>

To reference the banner view in code, use findViewById, as with any main.xml element:

BannerView myBanner = (BannerView) findViewById(R.id.gsBanner);

To request adds call

myBanner.refresh();

Now the problem is since i dont have an xml layout i cant figure out how i inflate the layout for the ad view?

Anyone have any ideas?

EDIT:

Ive seen someone do it like this in a tutorial online, but how can i inflate this in andengine?

try {
    String applicationId = Utils.scrapeIgnoreCase(externalParams, "<param name=\"id\">", "</param>");           
    GSSDK.initialize(context, applicationId);

    BannerView myBanner = new BannerView(context);          
    myBanner.setLayoutParams(view.getLayoutParams());
    myBanner.addListener(new GreyStripeBannerListener());           
    view.addView(myBanner);
    myBanner.refresh();
    myBanner.setOnClickListener(new  View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Click();
        }
    });

回答1:

I'm using AdMob but it should be similar.

Like @Sergey Benner referenced, you have to override onSetContentView in your activity, then create the RenderSurfaceView and your ad view manually.

First of all, create a FrameLayout to contain AndEngine's view and the ad view. Add AndEngine's view and create your ad view, then set the frame layout as the content view.

@Override
protected void onSetContentView() {
    //Creating the parent frame layout:
    final FrameLayout frameLayout = new FrameLayout(this);
    //Creating its layout params, making it fill the screen.
    final FrameLayout.LayoutParams frameLayoutLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,
                    FrameLayout.LayoutParams.FILL_PARENT);

    //Creating the banner view.
    BannerView bannerView = new BannerView(this);

    //....
    //Do any initiallizations on the banner view here.
    //....

    //Creating the banner layout params. With this params, the ad will be placed in the top of the screen, middle horizontally.
    final FrameLayout.LayoutParams bannerViewLayoutParams =
            new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
                    FrameLayout.LayoutParams.WRAP_CONTENT,
                    Gravity.TOP | Gravity.CENTER_HORIZONTAL);

    //Creating AndEngine's view.
    this.mRenderSurfaceView = new RenderSurfaceView(this);
    mRenderSurfaceView.setRenderer(mEngine, this);

    //createSurfaceViewLayoutParams is an AndEngine method for creating the params for its view.
    final android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =
            new FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());

    //Adding the views to the frame layout.
    frameLayout.addView(this.mRenderSurfaceView, surfaceViewLayoutParams);
    frameLayout.addView(bannerView, bannerViewLayoutParams);

    //Setting content view
    this.setContentView(frameLayout, frameLayoutLayoutParams);
}

Place this method in your BaseGameActivity class.