激励广告未加载
Rewarded Ads NOT LOADING
您好,
我正在尝试展示激励广告,但无法加载。我遵循了 AdMob 的官方教程(https://www.youtube.com/watch?v=CmrTF0hLsIk&t)。这是错误:
E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
I/Ads: This request is sent from a test device.
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reward);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadAd();
}
public void loadAd(){
this.rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
RewardedAdLoadCallback callback = new RewardedAdLoadCallback(){
@Override
public void onRewardedAdFailedToLoad(int i) {
super.onRewardedAdFailedToLoad(i);
Log.i(TAG, "OnRewardedAdFailedToLoad");
}
@Override
public void onRewardedAdLoaded() {
super.onRewardedAdLoaded();
Log.i(TAG, "OnRewardedTagLoaded");
}
};
this.rewardedAd.loadAd(new AdRequest.Builder().build(), callback);
}
public void showAd(){
if (this.rewardedAd.isLoaded()){
RewardedAdCallback callback = new RewardedAdCallback() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "OnUserEarnedReward");
}
@Override
public void onRewardedAdOpened() {
super.onRewardedAdOpened();
Log.i(TAG, "OnRewardAdOpened");
}
@Override
public void onRewardedAdClosed() {
super.onRewardedAdClosed();
Log.i(TAG, "OnRewardAdClosed");
}
@Override
public void onRewardedAdFailedToShow(int i) {
super.onRewardedAdFailedToShow(i);
Log.i(TAG, "OnRewardedAdFailedToShow");
}
};
this.rewardedAd.show(this, callback);
} else {
Log.i(TAG, "Ad not loaded.");
}
}
所有使用的 AdMob ID 仅用于测试,我在 AdMob 文档中获得了它们
在清单中,我添加了:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.gms:play-services-ads:18.2.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'
请帮助我,我只需要解决这个问题就可以完成我的应用程序。被这个错误卡了两周了,累死了,想放弃了。
你添加过测试设备id吗?在Logcat.
中过滤AdRequest.Builder.addTestDevice
很容易找到测试设备id
解决方案:
模拟器出错了!要加载广告,我们需要 Wi-Fi 连接。 API =< 25 的 AVD 无法连接到 wi-fi。所以我刚刚安装了 Android Oreo 并且成功了。
您好,
我正在尝试展示激励广告,但无法加载。我遵循了 AdMob 的官方教程(https://www.youtube.com/watch?v=CmrTF0hLsIk&t)。这是错误:
E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
I/Ads: This request is sent from a test device.
这是我的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reward);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
loadAd();
}
public void loadAd(){
this.rewardedAd = new RewardedAd(this, "ca-app-pub-3940256099942544/5224354917");
RewardedAdLoadCallback callback = new RewardedAdLoadCallback(){
@Override
public void onRewardedAdFailedToLoad(int i) {
super.onRewardedAdFailedToLoad(i);
Log.i(TAG, "OnRewardedAdFailedToLoad");
}
@Override
public void onRewardedAdLoaded() {
super.onRewardedAdLoaded();
Log.i(TAG, "OnRewardedTagLoaded");
}
};
this.rewardedAd.loadAd(new AdRequest.Builder().build(), callback);
}
public void showAd(){
if (this.rewardedAd.isLoaded()){
RewardedAdCallback callback = new RewardedAdCallback() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i(TAG, "OnUserEarnedReward");
}
@Override
public void onRewardedAdOpened() {
super.onRewardedAdOpened();
Log.i(TAG, "OnRewardAdOpened");
}
@Override
public void onRewardedAdClosed() {
super.onRewardedAdClosed();
Log.i(TAG, "OnRewardAdClosed");
}
@Override
public void onRewardedAdFailedToShow(int i) {
super.onRewardedAdFailedToShow(i);
Log.i(TAG, "OnRewardedAdFailedToShow");
}
};
this.rewardedAd.show(this, callback);
} else {
Log.i(TAG, "Ad not loaded.");
}
}
所有使用的 AdMob ID 仅用于测试,我在 AdMob 文档中获得了它们
在清单中,我添加了:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713" />
build.gradle:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.0.2'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.cardview:cardview:1.0.0'
implementation 'com.google.android.material:material:1.0.0'
implementation 'com.google.android.gms:play-services-ads:18.2.0'
implementation 'com.google.firebase:firebase-database:17.0.0'
implementation 'androidx.legacy:legacy-support-v4:1.0.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.0'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.1'
}
apply plugin: 'com.google.gms.google-services'
请帮助我,我只需要解决这个问题就可以完成我的应用程序。被这个错误卡了两周了,累死了,想放弃了。
你添加过测试设备id吗?在Logcat.
中过滤AdRequest.Builder.addTestDevice
很容易找到测试设备id
解决方案:
模拟器出错了!要加载广告,我们需要 Wi-Fi 连接。 API =< 25 的 AVD 无法连接到 wi-fi。所以我刚刚安装了 Android Oreo 并且成功了。