你好!
我想使用RewardedAd(admob)在我的应用中获得奖励。我在youtube(https://www.youtube.com/watch?v=CmrTF0hLsIk)上观看了官方的admob视频。这是我的代码:
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.");
}
}
但是我的广告没有加载。当我尝试showAd()时,logcat会显示以下消息:
01-17 19:27:00.144 27830-27830/com.example.quiz E/Ads: Google Mobile Ads SDK initialization functionality unavailable for this session. Ad requests can be made at any time.
01-17 19:27:08.968 27830-27830/com.example.quiz I/RewardActivity: Ad not loaded.
01-17 19:27:02.256 27830-27830/com.example.quiz I/Ads: This request is sent from a test device.
我正在使用Google的测试ID。我的代码有什么问题?为什么我的广告没有加载?我能做什么?
谢谢!
我遵循了同一教程,并检查了您的代码,这似乎是正确的,以防万一您想再次检查!在您按下载入按钮后,请尝试按几次显示按钮,因为有时广告会花费一些时间来载入。并且不要忘记从activity_main.xml设置按钮onClick函数。
这是MainActivity:
public class MainActivity extends AppCompatActivity {
private RewardedAd rewardedAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
}
public void showAd(View view) {
if(this.rewardedAd.isLoaded()){
RewardedAdCallback callback = new RewardedAdCallback() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
Log.i("MainActivity","Got Reward");
}
@Override
public void onRewardedAdOpened() {
super.onRewardedAdOpened();
Log.i("MainActivity","Opened");
}
@Override
public void onRewardedAdClosed() {
super.onRewardedAdClosed();
Log.i("MainActivity","Closed");
}
@Override
public void onRewardedAdFailedToShow(int i) {
super.onRewardedAdFailedToShow(i);
Log.i("MainActivity","Failed to show");
}
};
this.rewardedAd.show(this,callback);
}
else{
Log.i("MainActivity","Not Loaded");
}
}
public void loadAd(View view) {
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("MainActivity","Failed");
}
@Override
public void onRewardedAdLoaded() {
super.onRewardedAdLoaded();
Log.i("MainActivity","Loaded");
}
};
this.rewardedAd.loadAd(new AdRequest.Builder().build(),callback);
}
}
这是Android清单:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"/>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是问题:
apply plugin: 'com.android.application'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "jalaleddine.abdelbasset.admobtest"
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
implementation 'com.google.android.gms:play-services-ads:18.2.0'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。