Reputation: 154
Hello!
I want to get reward in my app using RewardedAd (admob). I followed official admob video on youtube (https://www.youtube.com/watch?v=CmrTF0hLsIk). This is my code:
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.");
}
}
But my ad isn't loading. When I try to showAd(), logcat shows me this message:
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.
I'm using Google's test IDs. What's wrong with my code? Why does my ads not loading? What can I do?
Thanks!
Upvotes: 0
Views: 1115
Reputation: 154
UPDATE:
I found the solution, virtual device version must be android 8.0 or higher bc it needs to have internet connection!
Upvotes: 0
Reputation: 11
I followed the same tutorial and i checked your code it seems to be right this is my code just in case you want to double check! And after you press the load button try to press the show button a couple times because sometimes the ad take some time to load. And don't forget to set the buttons onClick function from the activity_main.xml.
This is the 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);
}
}
This is the Android Manifest:
<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>
And this is the gradle:
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'
}
Upvotes: 1