2014-04-16 37 views
0

我的應用程序(OpenGL ES 2.0)目前有55個類,一個活動單一。在應用程序的某些部分禁用AdMob廣告

我有我自己的自定義場景管理器,我用它來在不同的場景之間切換(即不同的場景是:主菜單,級別選擇,1級,2級.... 20級,遊戲結束等。 )

因此,我將所有AdMob的東西放在我的Activity的onCreate()中;方法。所以目前,同一個廣告在整個應用中運行。

如何在不同場景中「關閉」廣告?

請記住,我的任何類都不能直接訪問Activity類本身。

任何建議,將不勝感激,因爲我發現使用AdMob非常具有挑戰性,它似乎不是很直觀,所以任何AdMob專家在那裏,您的意見將不勝感激!

我應該指出,我不使用XML,一切都以編程方式完成。

感謝

代碼

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

     // Create an ad. 
     adView = new AdView(this); 
     adView.setAdSize(AdSize.BANNER); 
     adView.setAdUnitId(AD_UNIT_ID); 

     // Add the AdView to the view hierarchy. The view will have no size 
     // until the ad is loaded. 
     RelativeLayout layout = new RelativeLayout(this); 
     layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); 

     // Create an ad request. Check logcat output for the hashed device ID to 
     // get test ads on a physical device. 
     AdRequest adRequest = new AdRequest.Builder() 
      .addTestDevice(TestDeviceID) 
      .build(); 

     // Start loading the ad in the background. 
     adView.loadAd(adRequest); 

     //Request full screen 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
     WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     //Create a displayMetrics object to get pixel width and height 
     metrics = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(metrics); 
     width = metrics.widthPixels; 
     height = metrics.heightPixels; 

     //Work out values for resizing screen while keeping aspect ratio 

     width = (int) Math.min(width, height * 1.702127659574468); 
     height = (int) Math.min(height, width/1.702127659574468); 

     //Create and set GL view (OpenGL View) 
     myView = new MyGLSurfaceView(MainActivity.this); 

     RelativeLayout.LayoutParams adParams = 
       new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
       RelativeLayout.LayoutParams.WRAP_CONTENT); 
       adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); 
       adParams.addRule(RelativeLayout.CENTER_HORIZONTAL); 

     //Set the colour if we don't, the ad won't show (bug?) 
     adView.setBackgroundColor(Color.BLACK); 
     layout.addView(myView); 
     layout.addView(adView, adParams); 

     //Create a copy of the Bundle 
     if (savedInstanceState != null){ 
      newBundle = new Bundle(savedInstanceState);   
     } 


     setContentView(layout); 

} 

回答

0

首先你要通過編程方式做你的佈局造成自己很大的痛苦(我對這件就在昨天晚上的演講)。你應該真的考慮理解和使用XML佈局。

但是要回答您的問題,您需要爲想要控制是否顯示廣告的類找到一種方法,或者找不到某種方法將消息發送到活動。我建議使用由Activity中的內部類實現的接口來注入它們。

界面需要看起來像這樣: - startShowingAds - 這應該開始請求廣告,並使AdView可見。 - stopShowingAds - 這應該暫停廣告顯示和隱藏AdView。

0

要暫時隱藏您只需隱藏AdView的廣告,最終會調用暫停(),即使我認爲隱藏的廣告視圖未刷新(您可以檢查日誌)。

關於如何通過代碼訪問AdView對象,取決於您可以從中訪問的內容。如果您只是在繪製解決方案的視圖中可以將AdView對象存儲在視圖的標記中(請參閱setTag()和getTag():https://developer.android.com/reference/android/view/View.html#getTag%28int%29)。

相關問題