2017-05-16 141 views
-1

我跟着教程都在這裏: https://developers.google.com/android/reference/com/google/android/gms/ads/AdViewAdMob廣告不可見(使用LibGDX)

在這裏: https://github.com/libgdx/libgdx/wiki/Admob-in-libgdx

一切看起來像它應該顯示出來,但是當我啓動我的應用程序(包括上我的模擬器和一個實際的設備)沒有廣告出現。

這裏是我的代碼:

import android.os.Bundle; 
import android.view.View; 
import android.view.Window; 
import android.view.WindowManager; 
import android.widget.LinearLayout; 
import android.widget.RelativeLayout; 

import com.badlogic.gdx.backends.android.AndroidApplication; 
import com.badlogic.gdx.backends.android.AndroidApplicationConfiguration; 
import com.google.android.gms.ads.AdRequest; 
import com.google.android.gms.ads.AdSize; 
import com.google.android.gms.ads.AdView; 
import com.google.android.gms.ads.MobileAds; 

/** 
* Android's way to initialize the game. 
*/ 
public class AndroidLauncher extends AndroidApplication { 


private AdView adView; 

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

    MobileAds.initialize(this, "################"); 

    RelativeLayout layout = new RelativeLayout(this); 

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

    AndroidApplicationConfiguration config = new AndroidApplicationConfiguration(); 

    View gameView = initializeForView(new DoubleDodge(new AndroidDatabaseAccess()), config); 

    // Create and setup the AdMob view 
    adView = new AdView(this); 
    adView.setAdSize(AdSize.SMART_BANNER); 
    adView.setAdUnitId("#############"); 
    adView.setVisibility(AdView.VISIBLE); 

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

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


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

    // Start loading the ad. 
    adView.loadAd(adRequestBuilder.build()); 


    // Add the AdMob view 
    RelativeLayout.LayoutParams adParams = 
      new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
        RelativeLayout.LayoutParams.WRAP_CONTENT); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); 
    adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); 

    // Add the AdView to the view hierarchy. 
    layout.addView(adView, adParams); 

    // Hook it all up 
    setContentView(layout); 

    //initialize(new DoubleDodge(new AndroidDatabaseAccess()), config); 
} 

@Override 
public void onResume() { 
    super.onResume(); 

    // Resume the AdView. 
    adView.resume(); 
} 

@Override 
public void onPause() { 
    // Pause the AdView. 
    adView.pause(); 

    super.onPause(); 
} 

@Override 
public void onDestroy() { 
    // Destroy the AdView. 
    adView.destroy(); 

    super.onDestroy(); 
} 
} 

感謝您的幫助。

+0

[java-libgdx build.gradle可能重複的AdMob不一樣](http://stackoverflow.com/questions/43301569/java-libgdx-build-gradle-is-not-the-same-for -admob) – Aryan

回答

0

FIRST:

private InterstitialAd mInterstitialAd; 

第二:

mInterstitialAd = new InterstitialAd(this); 
      mInterstitialAd.setAdUnitId(getResources().getString(R.string.interstitial_ad_unit_id)); 
      AdRequest adRequestInter = new AdRequest.Builder().build(); 
      mInterstitialAd.setAdListener(new AdListener() { 
       @Override 
       public void onAdLoaded() { 
        mInterstitialAd.show(); 
       } 
      }); 

      mInterstitialAd.loadAd(adRequestInter); 

OR

AdView mAdView = (AdView) findViewById(R.id.adView); 
     AdRequest adRequest = new AdRequest.Builder().build(); 
     mAdView.loadAd(adRequest); 

編碼快樂

+0

我希望橫幅廣告不是插頁式廣告,我希望以編程方式設置廣告的位置而不使用XML。感謝您的幫助。 –

0

想通了。事實證明,只有第一輪廣告(前60秒)沒有顯示。爲了解決這個問題,我瀏覽堆棧溢出,發現設置AdView中Color.TRANSPARENT的背景修復該問題:

adView.setBackgroundColor(Color.TRANSPARENT); 

謝謝大家的幫助,我希望這可以幫助別人那裏。