2012-01-20 77 views
7

我想在AndEngine中使用Greystrip顯示廣告。顯示AndEngine中的廣告

我不明白這是如何做的,因爲它沒有使用佈局來膨脹視圖,但精靈。

我使用BaseGameActivity爲每個場景創建我想要顯示添加的應用程序。

在GreyStrip這也是他們告訴你在你的應用廣告整合..

之前在應用程序中添加GSSDK呼叫,您需要 納入SDK到您的AndroidManifest.xml。在該部分中添加以下 ,用對應用程序唯一的程序包標識替換 。此 內容提供商管理廣告內容的本地存儲,而 活動管理廣告顯示。

<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> 

要初始化SDK的Greystripe,調用initialize方法在 啓動。這應該在您的應用程序的onCreate() 方法中完成。此調用將產生一個後臺線程來初始化我們的 活動,然後將控制返回到您的應用程序。在這種背景下, Greystripe活動將下載廣告以及任何SDK更新。 參數:ctx:您的應用程序上下文實例appId:使用應用程序註冊期間提供的 appId。提供無效的appId 將導致SDK顯示錯誤通知廣告。

public static GSSDK initialize(Context ctx, String appId) 

要使用的旗幟,將在main.xml中的文件如下:

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

要引用的旗幟視圖代碼,使用findViewById,與任何 main.xml元素:

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

,請求增加了呼叫

myBanner.refresh(); 

現在的問題是,因爲我沒有一個XML佈局我無法弄清楚如何誇大的廣告視圖的佈局?

任何人有任何想法?

編輯:

我見過有人做這樣的一個在線教程,但我怎麼能在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(); 
     } 
    }); 
+0

你有什麼樣的佈局?至少你必須有某種線性/相對佈局才能在屏幕上排列組件。如果是這樣,只需創建LayoutParams lp ...然後mymainview.addView(myBanner,lp);並繼續下去。 –

+0

使用GreyStripe可以使用BannerView。正如我在我的問題中所述。檢查我的更新..問題是我無法弄清楚如何將這與AndEngine –

+0

什麼是你在AdView()在AdView和你如何設置你的setContentView()在那裏? –

回答

6

我使用的是AdMob,但它應該類似。

像@Sergey Benner引用,您必須在您的活動中覆蓋onSetContentView,然後手動創建RenderSurfaceView和廣告視圖。

首先,創建一個FrameLayout來包含AndEngine的視圖和廣告視圖。 添加AndEngine的視圖並創建您的廣告視圖,然後將框架佈局設置爲內容視圖。

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

將此方法放置在您的BaseGameActivity類中。

+1

Jong節省了一天的時間.. (this.mAdView,bannerViewLayoutParams); 應該是frameLayout.addView(this.bannerView,bannerViewLayoutParams);正確? –

+1

是的,你可以使用frameLayout.addView(this.mAdView,bannerViewLayoutParams)我只是忘了改變它(這是從我的遊戲中的一種方法,但我用AdMob的'AdView',我現在就修復它:)) – Jong

+0

是你的遊戲在市場上?我想嘗試一下。 –