我跟着教程都在這裏: 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();
}
}
感謝您的幫助。
[java-libgdx build.gradle可能重複的AdMob不一樣](http://stackoverflow.com/questions/43301569/java-libgdx-build-gradle-is-not-the-same-for -admob) – Aryan