我正在尝试在活动Two
上实施插页式广告。但是我由于某种原因我的广告没有加载。我是Java的新手,并尝试遵循AdMob的说明,但是我仍然不确定我缺少什么以及为什么未加载广告单元。
这是我的活动One
:
Button goFree;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId("ca-app-pub-3940256099942544/xxxxxxxx");
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
startActivity(new Intent(One.this, Two.class));
mInterstitialAd.loadAd(new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build());
}
});
Button goFree = (Button) findViewById(R.id.goFree);
goFree.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startTwo();
}
});
}
public void startTwo() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Log.d("TAG", "The interstitial wasn't loaded yet.");
startActivity(new Intent(One.this, Two.class));
}
}
当我单击“按钮”转到第二个活动时,它将记录The interstitial wasn't loaded yet.
。我在这里想念什么?
答案 0 :(得分:1)
您忘记添加,
MobileAds.initialize(LoginActivity.this,
"ca-app-pub-3940256099942544~3347511713");
请参阅:-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
MobileAds.initialize(LoginActivity.this,
"ca-app-pub-3940256099942544~3347511713");
//rest of code here
}
答案 1 :(得分:1)
您可以尝试一下。
声明全局变量:
InterstitialAd mInterstitialAd;
下面的代码用于oncreate或根据需要:
mInterstitialAd = new InterstitialAd(this);
// set the ad unit ID
mInterstitialAd.setAdUnitId(getString(R.string.interstitial_full_screen));
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).addTestDevice("5ADF8BFD1F030BF7ED41A52D88C63B9C").build();
mInterstitialAd.loadAd(adRequest);
mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
showInterstitial();
}
@Override
public void onAdClosed() {
}
@Override
public void onAdFailedToLoad(int errorCode) {
}
@Override
public void onAdLeftApplication() {
}
@Override
public void onAdOpened() {
}
});
按以下方法显示。
private void showInterstitial() {
if (mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
}
}
使用此ID一段时间:
<string name="interstitial_full_screen">ca-app-pub-2330175674430713/9772460509</string>
有时您的admob代码无法正常工作。尝试使用此方法,确定它会起作用。
谢谢您,希望对您有所帮助。