2015-04-16 106 views
0

我在this教程之後製作了一款安卓遊戲,我想在將其上傳到Google Play商店之前添加admob廣告。我嘗試使用this指南,但我很困惑,因爲我的應用程序不使用layout.xml,只有一個活動。是否可以在不切換活動的情況下在我的應用的某些部分中投放廣告?如果沒有,將我的應用程序轉換爲使用多個活動的應用程序有多難?如有必要,我可以提供任何其他信息。如何通過一項活動將admob廣告添加到我的遊戲中?

這裏是我的活動(我有幾個變化的子類):

package com.kilobolt.framework.implementation; 
 

 

 
import android.app.Activity; 
 
import android.content.Context; 
 
import android.content.res.Configuration; 
 
import android.graphics.Bitmap; 
 
import android.graphics.Bitmap.Config; 
 
import android.os.Bundle; 
 
import android.os.PowerManager; 
 
import android.os.PowerManager.WakeLock; 
 
import android.view.Window; 
 
import android.view.WindowManager; 
 

 
import com.google.android.gms.ads.AdRequest; 
 
import com.google.android.gms.ads.AdSize; 
 
import com.google.android.gms.ads.AdView; 
 
import com.kilobolt.framework.Audio; 
 
import com.kilobolt.framework.FileIO; 
 
import com.kilobolt.framework.Game; 
 
import com.kilobolt.framework.Graphics; 
 
import com.kilobolt.framework.Input; 
 
import com.kilobolt.framework.Screen; 
 

 
public abstract class AndroidGame extends Activity implements Game { 
 
    AndroidFastRenderView renderView; 
 
    Graphics graphics; 
 
    Audio audio; 
 
    Input input; 
 
    FileIO fileIO; 
 
    Screen screen; 
 
    WakeLock wakeLock; 
 
    private AdView mAdView; 
 

 
    @Override 
 
    public void onCreate(Bundle savedInstanceState) { 
 
     super.onCreate(savedInstanceState); 
 

 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
 

 
     boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT; 
 
     int frameBufferWidth = isPortrait ? 480: 800; 
 
     int frameBufferHeight = isPortrait ? 800: 480; 
 
     Bitmap frameBuffer = Bitmap.createBitmap(frameBufferWidth, 
 
       frameBufferHeight, Config.RGB_565); 
 
     
 
     float scaleX = (float) frameBufferWidth 
 
       /getWindowManager().getDefaultDisplay().getWidth(); 
 
     float scaleY = (float) frameBufferHeight 
 
       /getWindowManager().getDefaultDisplay().getHeight(); 
 

 
     renderView = new AndroidFastRenderView(this, frameBuffer); 
 
     graphics = new AndroidGraphics(getAssets(), frameBuffer); 
 
     fileIO = new AndroidFileIO(this); 
 
     audio = new AndroidAudio(this); 
 
     input = new AndroidInput(this, renderView, scaleX, scaleY); 
 
     screen = getInitScreen(); 
 
     setContentView(renderView); 
 
     
 
     PowerManager powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE); 
 
     wakeLock = powerManager.newWakeLock(PowerManager.FULL_WAKE_LOCK, "MyGame"); 
 
       
 
    } 
 

 
    @Override 
 
    public void onResume() { 
 
     super.onResume(); 
 
     wakeLock.acquire(); 
 
     screen.resume(); 
 
     renderView.resume(); 
 
    } 
 

 
    @Override 
 
    public void onPause() { 
 
     super.onPause(); 
 
     wakeLock.release(); 
 
     renderView.pause(); 
 
     screen.pause(); 
 

 
     if (isFinishing()) 
 
      screen.dispose(); 
 
    } 
 

 
    @Override 
 
    public Input getInput() { 
 
     return input; 
 
    } 
 

 
    @Override 
 
    public FileIO getFileIO() { 
 
     return fileIO; 
 
    } 
 

 
    @Override 
 
    public Graphics getGraphics() { 
 
     return graphics; 
 
    } 
 

 
    @Override 
 
    public Audio getAudio() { 
 
     return audio; 
 
    } 
 

 
    @Override 
 
    public void setScreen(Screen screen) { 
 
     if (screen == null) 
 
      throw new IllegalArgumentException("Screen must not be null"); 
 

 
     this.screen.pause(); 
 
     this.screen.dispose(); 
 
     screen.resume(); 
 
     screen.update(0); 
 
     this.screen = screen; 
 
    } 
 
    
 
    public Screen getCurrentScreen() { 
 

 
    \t return screen; 
 
    } 
 
}

+0

好吧,首先,先生,你有兩個選擇間質和AD瀏覽,所以這是你指的是?並請發佈您希望放置廣告的很大一部分活動,所以人人網可以幫助您 – Elltz

+0

謝謝,我將我的活動添加到了我的文章中! –

+1

sir,勾選[post](https://developers.google.com/mobile-ads-sdk/docs/admob/android/interstitial)..如果您喜歡使用佈局,那麼您將使用'AdView',下面的答案詳細說明了這一點,但你不想使用佈局,那麼你可以使用'interstitial'請檢查** post **它闡述了** Interstitial Add **以及如何將它放入你的應用程序。嘗試一下,如果該職位解決您的要求,請刪除該問題,基於stackoverflow標準它是一個貧窮的問題,因爲缺乏研究.. – Elltz

回答

0

它可以在不添加XML廣告。看到這個official document。下面的代碼片段來自文檔。

LinearLayout layout = new LinearLayout(this); 
layout.setOrientation(LinearLayout.VERTICAL); 
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT)); 

// Create a banner ad. The ad size and ad unit ID must be set before calling loadAd. 
mAdView = new AdView(this); 
mAdView.setAdSize(AdSize.SMART_BANNER); 
mAdView.setAdUnitId("myAdUnitId"); 

// Create an ad request. 
AdRequest.Builder adRequestBuilder = new AdRequest.Builder(); 

// Optionally populate the ad request builder. 
adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR); 

// Add the AdView to the view hierarchy. 
layout.addView(mAdView); 

// Start loading the ad. 
mAdView.loadAd(adRequestBuilder.build()); 
setContentView(layout); 
+0

謝謝!有沒有辦法做到這一點,而不使用佈局?當我創建佈局時,我所看到的只是一個空白屏幕和我的廣告。 –

+0

使用'setLayoutParams'來相應地調整佈局。我編輯了答案。您可以更改LinearLayout.Params的值以在屏幕上設置佈局。 – zzas11

+0

你可以按照這個教程佈局設置[鏈接](http://startandroid.ru/en/lessons/complete-list/220-lesson-16-creating-layout-programmatically-layoutparams.html) – zzas11

相關問題