在 AndEngine 中显示广告 [英] Displaying Ad In AndEngine

查看:21
本文介绍了在 AndEngine 中显示广告的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 AndEngine 中使用 Greystrip 显示广告.

我无法弄清楚这是如何完成的,因为它不使用布局来膨胀视图,而是使用精灵.

我使用 BaseGameActivity 为我想要显示的每个场景创建我的应用程序.

在 GreyStrip 中,这是他们告诉您在应用程序中集成广告的方式..

<块引用>

在您的应用程序中将调用添加到 GSSDK 之前,您需要将 SDK 合并到您的 AndroidManifest.xml 中.添加以下内容在该部分,替换具有对您的应用程序唯一的包标识符.这个Content Provider 管理广告内容的本地存储,而活动管理广告展示.

 <activity android:name="com.greystripe.android.sdk.AdView"android:configChanges="keyboard|keyboardHidden|orientation" ><意图过滤器><category android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动>

<块引用>

要初始化 Greystripe SDK,请在以下位置调用 initialize 方法启动.这应该在您的应用程序的 onCreate() 中完成方法.这个调用将产生一个后台线程来初始化我们的活动,然后将控制权返回给您的应用程序.在此背景下,Greystripe 活动将下载广告以及任何 SDK 更新.参数: ctx:您的应用程序上下文实例 appId:使用在应用注册期间提供的 appId.提供无效的 appId会导致 SDK 显示错误通知广告.

 public static GSSDK initialize(Context ctx, String appId)

<块引用>

要使用横幅,请将以下内容放入 main.xml 文件中:

<块引用>

要在代码中引用横幅视图,请使用 findViewById,就像任何main.xml 元素:

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

请求添加调用

myBanner.refresh();

现在的问题是,因为我没有 xml 布局,所以我无法弄清楚如何为广告视图填充布局?

有人有什么想法吗?

我在网上的教程中看到有人这样做,但我如何在 andengine 中对此进行充气?

尝试{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() {@覆盖public void onClick(View v) {点击();}});

解决方案

我正在使用 AdMob,但它应该是类似的.

就像@Sergey Benner 引用的那样,您必须在您的活动中覆盖 onSetContentView,然后手动创建 RenderSurfaceView 和您的广告视图.

首先,创建一个 FrameLayout 来包含 AndEngine 的视图和广告视图.添加 AndEngine 的视图并创建您的广告视图,然后将框架布局设置为内容视图.

@Override受保护的无效 onSetContentView() {//创建父框架布局:最终 FrameLayout frameLayout = new FrameLayout(this);//创建其布局参数,使其填满屏幕.最终 FrameLayout.LayoutParams frameLayoutLayoutParams =新的 FrameLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT,FrameLayout.LayoutParams.FILL_PARENT);//创建横幅视图.BannerView bannerView = new BannerView(this);//....//在此处对横幅视图进行任何初始化.//....//创建横幅布局参数.使用此参数,广告将放置在屏幕顶部,水平中间.最终 FrameLayout.LayoutParams bannerViewLayoutParams =新的 FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT,重力.TOP |Gravity.CENTER_HORIZONTAL);//创建AndEngine的视图.this.mRenderSurfaceView = new RenderSurfaceView(this);mRenderSurfaceView.setRenderer(mEngine, this);//createSurfaceViewLayoutParams 是一个 AndEngine 方法,用于为其视图创建参数.最终 android.widget.FrameLayout.LayoutParams surfaceViewLayoutParams =新 FrameLayout.LayoutParams(super.createSurfaceViewLayoutParams());//将视图添加到框架布局.frameLayout.addView(this.mRenderSurfaceView,surfaceViewLayoutParams);frameLayout.addView(bannerView,bannerViewLayoutParams);//设置内容视图this.setContentView(frameLayout, frameLayoutLayoutParams);}

将此方法放在您的 BaseGameActivity 类中.

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();
        }
    });

解决方案

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.

这篇关于在 AndEngine 中显示广告的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆