您好我正在尝试使用admob展示interstital广告,但它没有展示广告。 我的代码如下
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
private static final String AD_UNIT_ID="ca-app-pub-21740939xxxxx";
private InterstitialAd interstitial;
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
displayInterstitial();
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}
在清单中我声明了权限:
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
但它没有显示。请帮我解决问题
答案 0 :(得分:0)
您只能在加载后展示广告。当您致电displayInterstitial()
时,您的广告可能无法加载。注册一个监听器,它会在加载完成时告诉你。
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
}
});
答案 1 :(得分:0)
我猜您在展示广告时尚未加载,请尝试使用
interstitial= new InterstitialAd(this);
interstitial.setAdUnitId(AD_UNIT_ID);
interstitial.loadAd(new AdRequest.Builder().build();
}
interstitial.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
super.onAdLoaded();
interstitial.show(); // you can call this anywhere you want
}
@Override
public void onAdFailedToLoad(int errorCode) {
super.onAdFailedToLoad(errorCode);
}
});
答案 2 :(得分:0)
我使用以下代码显示插页式广告。它对我来说很完美。
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
private InterstitialAd interstitial;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
interstitial = new InterstitialAd(this);
interstitial.setAdUnitId(“**************************”);
AdRequest adRequest = new AdRequest.Builder().build();
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener(){
public void onAdLoaded(){
displayInterstitial();
}
});
}
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
}