我已经实施了横幅广告并且有效! 我希望在MainActivity中实现全屏广告
代码:
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.InterstitialAd;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.widget.Toast;
public class Servizi extends FragmentActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.servizi);
InterstitialAd mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Toast.makeText(Servizi.this,
"The interstitial is loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Proceed to the next level.
Toast.makeText(Servizi.this,
"The interstitial is CLOSED", Toast.LENGTH_SHORT).show();
}
});
}
}
没有错误,但我无法看到此广告! 我的单位ID是正确的。 我不会在此广告中使用XML文件。 救命! :(
答案 0 :(得分:1)
请确保您已完成http://developer.android.com/google/play-services/setup.html#Setup
编辑:
@Override
public void onReceiveAd(Ad ad) {
if (ad == interstitial) {
interstitial.show();
}
}
AdRequest adRequest = new AdRequest();
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("myAdUnitId");
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Toast.makeText(MyActivity.this,
"The interstitial is loaded", Toast.LENGTH_SHORT).show();
}
@Override
public void onAdClosed() {
// Proceed to the next level.
goToNextLevel();
}
});
取自官方文件http://developer.android.com/reference/com/google/android/gms/ads/InterstitialAd.html
答案 1 :(得分:0)
很简单,你是:
将以下内容添加到onCreate
// Create ad request.
final AdRequest adRequest = new AdRequest.Builder().build();
// Begin loading your interstitial.
interstitial.loadAd(adRequest);
添加显示插页式广告的方法:
// Invoke displayInterstitial() when you are ready to display an interstitial.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
并在应用中的自然断点处调用displayInterstitial
。