[英]Android Studio - Interstitial ads load issue
所以我正在嘗試將插頁式廣告添加到我的項目中。
這是我的代碼對於當前JHAActivity.java
的樣子:
注意:在我的項目中,我使用的是我自己的廣告單元 ID,來自 apps.admob.com insted of ca-app-pub-1111111111111111/2222222222
。
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.RequestConfiguration;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
public class JHAActivity extends AppCompatActivity {
private Button jhaButton;
// private static final String AD_UNIT_ID = "ca-app-pub-3940256099942544/1033173712"; Test AD
private static final String AD_UNIT_ID = "ca-app-pub-1111111111111111/2222222222";
private InterstitialAd mInterstitialAd;
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// R.menu.mymenu is a reference to an xml file named mymenu.xml which should be inside your res/menu directory.
// If you don't have res/menu, just create a directory named "menu" inside res
getMenuInflater().inflate(R.menu.mymenu, menu);
return super.onCreateOptionsMenu(menu);
}
// handle button activities
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.mybutton) {
// do something here
if (Locale.getDefault().getLanguage().equals("en")) { // If English
new AlertDialog.Builder(this)
.setTitle(R.string.rules)
.setMessage(rulesENG)
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton(android.R.string.ok, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
} else { // Else if not English
new AlertDialog.Builder(this)
.setTitle(R.string.rules)
.setMessage(rules)
// A null listener allows the button to dismiss the dialog and take no further action.
.setNegativeButton(android.R.string.ok, null)
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jha);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Initialize the Mobile Ads SDK.
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(AD_UNIT_ID);
mInterstitialAd.loadAd(new AdRequest.Builder().build());
mInterstitialAd.setAdListener(new AdListener() {
@Override
public void onAdClosed() {
// Load the next interstitial.
mInterstitialAd.loadAd(new AdRequest.Builder().build());
}
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
}
@Override
public void onAdOpened() {
// Code to be executed when the ad is displayed.
}
@Override
public void onAdClicked() {
// Code to be executed when the user clicks on an ad.
}
@Override
public void onAdLeftApplication() {
// Code to be executed when the user has left the app.
}
});
jhaButton = (Button) findViewById(R.id.button);
jhaButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openJHA();
}
});
}
private void showInterstitial() {
// Show the ad if it's ready. Otherwise toast and restart the game.
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
// Toast.makeText(this, "Ad did not load", Toast.LENGTH_SHORT).show();
Log.d("TAG", "The interstitial wasn't loaded yet.");
}
}
int counter = 0;
private void openJHA() {
counter++;
if (counter == 5) {
showInterstitial(); // Display AdMob AD
counter = 0;
}
}
}
所以基本上當使用ca-app-pub-3940256099942544/1033173712
作為廣告單元 ID 時,每次我按下jhaButton
5 次時它都會顯示測試廣告,但是當使用我自己的廣告單元 ID 時,廣告根本不會顯示. 在運行日志中它只是說: D/TAG: The interstitial wasn't loaded yet.
. 我在這里做錯了什么?
@Override
public void onAdLoaded() {
// Code to be executed when an ad finishes loading.
}
@Override
public void onAdFailedToLoad(LoadAdError adError) {
// Code to be executed when an ad request fails.
}
您的mInterstitialAd.loadAd(xxx)
嘗試加載一次,但通常是onAdFailedToLoad(LoadAdError adError)
返回,如果您沒有像mInterstitialAd.loadAd
再次嘗試,它將停止並且永遠不會跳轉到onAdLoaded()
. 因此,您的mInterstitialAd.isLoaded()
幾乎會返回 false。
每次要使用mInterstitialAd.showAd()
時,請先檢查if (.mInterstitialAd.isLoaded()){mInterstitialAd.showAd() }
最大的問題是最近的 Admob 使用了 SDK v19.7.0。 與IntersitialAd相關的代碼需要再次修改......而且我不知道為什么回調會丟失關於onAdClicked的onAdClicked
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.