问题描述
我发誓我已经尝试了很多方法来解决这个问题,但我很累,现在是星期六,我仍然只在解决这个问题......
请帮忙!!
- 我将我的设备注册为测试设备
- 几周前我的 admob 帐户已通过验证
- Google 测试广告在同一台设备上运行良好
- 在两台真实设备上测试未成功
这是我的 myactivity.java:
MobileAds.initialize(this,new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this,"Ad did not load",Toast.LENGTH_SHORT).show();
}
// Create the InterstitialAd and set the adUnitId.
mInterstitialAd = new InterstitialAd(this);
// Defined in res/values/strings.xml
mInterstitialAd.setAdUnitId(AD_UNIT_ID);
RequestConfiguration configuration = new RequestConfiguration.Builder().setTestdeviceids(Arrays.asList("BDB.....")).build();
MobileAds.setRequestConfiguration(configuration);
mInterstitialAd .setAdListener(
new AdListener() {
@Override
public void onAdLoaded() {
Toast.makeText(MainActivity.this,"onAdLoaded()",Toast.LENGTH_SHORT).show();
}
@Override
public void onAdFailedToLoad(LoadAdError loadAdError) {
String error =
String.format(
"domain: %s,code: %d,message: %s",loadAdError.getDomain(),loadAdError.getCode(),loadAdError.getMessage());
Toast.makeText(
MainActivity.this,"onAdFailedToLoad() with error: " + error,Toast.LENGTH_SHORT)
.show();
}
@Override
public void onAdClosed() {
//startGame();
}
});
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
mInterstitialAd.show();
} else {
Toast.makeText(this,Toast.LENGTH_SHORT).show();
}
以及应用的 gradle :
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.android.application'
android {
compileSdkVersion 30
buildToolsversion "30.0.3"
defaultConfig {
applicationId "com....."
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
multiDexEnabled true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
useLibrary 'org.apache.http.legacy'
}
dependencies {
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.brewtab.json:json:1.0.0'
implementation 'com.android.volley:volley:1.1.0'
implementation 'com.google.firebase:firebase-ads:19.7.0'
implementation 'com.google.firebase:firebase-auth:20.0.2'
implementation 'com.google.android.gms:play-services-ads:19.7.0'
implementation 'androidx.appcompat:appcompat:1.2.0'
}
解决方法
我将这种简化的解决方案用于我的插页式广告,它始终有效,但请确保您使用测试单元 ID
//variables
private InterstitialAd interstitialAd;
//place this in your oncreate method
public void loadAD()
{
interstitialAd= new InterstitialAd(this);
interstitialAd.setAdUnitId(getString(R.string.ad_id));
AdRequest adRequest = new AdRequest.Builder().build();
interstitialAd.loadAd(adRequest);
}
//place this method where you want your ad to load (ex: onclick of a button)
public void showAd()
{
if (interstitialAd.isLoaded())
{
interstitialAd.show();
System.out.println("add shown");
}
else
{
Log.d("TAG","The interstitial wasn't loaded yet.");
}
}