#If TARGET="android" Or TARGET="ios"
Import brl.admob
#End
Function hideAd()
#If TARGET="android" Or TARGET="ios"
Admob.GetAdmob().HideAdView()
#End
End Function
Function showAd()
#If TARGET="android" Or TARGET="ios"
Admob.GetAdmob().ShowAdView(2,5)
#End
End Function
D/[Cerberus](11855): Google Mobile Ads SDK
D/[Cerberus](11855): BBAdmobRewarded MobileAds.initialize
D/[Cerberus](11855): BBAdmobRewarded startAd()
E/[Cerberus](11855): BBAdmobRewarded run() -> _rewardedAd = null
D/[Cerberus](11855): BBAdmobRewarded adRequest
D/[Cerberus](11855): BBAdmobRewarded onAdLoaded
I/[Cerberus](11855): Showing rewarded ad...
D/[Cerberus](11855): BBAdmobRewarded ShowAdViewRewarded() -> loaded = true
D/[Cerberus](11855): BBAdmobRewarded run() -> _rewardedAd.show
D/[Cerberus](11855): BBAdmobRewarded FullScreenContentCallback -> The ad was shown.
D/[Cerberus](11855): BBAdmobRewarded adRequest
D/[Cerberus](11855): BBAdmobRewarded onAdLoaded
D/[Cerberus](11855): The user earned the reward.
D/[Cerberus](11855): BBAdmobRewarded FullScreenContentCallback -> The ad was dismissed.
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="real_app_id_generated_by_admob_here"/>
maven {
url "https://maven.google.com"
}
mavenCentral()
#Rem
SETUP NEEDED:
(1) Open [project build]\android\gradletemplate\app\src\main\AndroidManifest.xml and add
just before ${ANDROID_MANIFEST_APPLICATION}:
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="real_app_id_generated_by_admob_here"/>
(2) Open [project build]/android/gradletemplate/build.gradle
replace every
maven {
url "https://maven.google.com"
}
with
mavenCentral()
#End
Import mojo.app
Import mojo.graphics
Import mojo.input
#If TARGET="android"
#If ANDROID_LIBGOOGLEPLAY_AVAILABLE
Import brl.admob
#End
Import "native/rewardedads.java"
#End If
Extern
Class AdmobRewarded Extends Null = "BBAdmobRewarded"
Function GetAdmobRewarded:AdmobRewarded(adUnitId:String)
Method ShowAdViewRewarded:Void()
Method isLoaded:Bool() ' was the ad loaded?
Method earnedReward:Bool() ' did the user earned the reward?
Method isClosed:Bool() ' was the ad closed?
End Class
Public
#If TARGET="android"
#ANDROID_APP_LABEL="Rewarded ads"
#ANDROID_APP_PACKAGE="com.icongames.rewardedAdsTest"
#ANDROID_SCREEN_ORIENTATION="sensorPortrait" '"user", "portrait", "landscape"
#ANDROID_VERSION_CODE="001"
#ANDROID_VERSION_NAME="0.01"
' test ads
#ADMOB_PUBLISHER_ID = "ca-app-pub-3940256099942544/5224354917"
#End
Class Game Extends App
Global IDLE:Int = 0
Global AD_VISIBLE:Int = 1
Global AD_CLOSED:Int = 2
Global UPDATE_RATE:Int = 30
Field state:Int
Field admobRewarded:AdmobRewarded
Method OnCreate()
#If TARGET="android"
' test ads
admobRewarded = AdmobRewarded.GetAdmobRewarded("ca-app-pub-3940256099942544/5224354917")
#End
SetUpdateRate UPDATE_RATE
state = IDLE
End Method
Method OnUpdate()
Select state
Case IDLE
If TouchHit(0) And admobRewarded.isLoaded() Then
Print "Showing rewarded ad..."
admobRewarded.ShowAdViewRewarded()
state = AD_VISIBLE
End If
Case AD_VISIBLE
If admobRewarded.isClosed() Then
state = AD_CLOSED
End If
Case AD_CLOSED
If admobRewarded.earnedReward() Then
Print "Yay! Reward earned"
Else
Print "Sorry, no reward."
End If
state = IDLE
End Select
End Method
Method OnRender()
Cls(0,0,0)
Select state
Case IDLE
If admobRewarded.isLoaded() Then
SetColor 255,255,255
DrawText "Touch the screen to open the ad!", 10,10
End If
Case AD_VISIBLE
Case AD_CLOSED
End Select
End Method
End Class
Function Main()
New Game()
End Function
import com.google.android.gms.ads.rewarded.RewardedAd;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import androidx.annotation.NonNull;
import com.google.android.gms.ads.AdError;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.FullScreenContentCallback;
import com.google.android.gms.ads.LoadAdError;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.OnUserEarnedRewardListener;
import com.google.android.gms.ads.rewarded.RewardItem;
import com.google.android.gms.ads.rewarded.RewardedAd;
import com.google.android.gms.ads.rewarded.RewardedAdLoadCallback;
class BBAdmobRewarded implements Runnable {
static BBAdmobRewarded _admob;
static Activity _activity;
private RewardedAd _rewardedAd;
boolean loaded;
boolean earned;
boolean closed;
String adUnitId;
private static final String TAG = "[Cerberus]";
// creates an instance of the object and start the thread
static public BBAdmobRewarded GetAdmobRewarded(String adUnitId){
Log.d(TAG, "Google Mobile Ads SDK");
if( _admob==null ) _admob=new BBAdmobRewarded();
_activity = BBAndroidGame.AndroidGame().GetActivity();
if( Variables.__bb__MobileAds_init == false ) {
Log.d(TAG, "BBAdmobRewarded MobileAds.initialize");
MobileAds.initialize(_activity, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});
Variables.__bb__MobileAds_init = true ;
}
_admob.startAd(adUnitId);
return _admob;
}
public boolean isLoaded() { return loaded; }
public boolean isClosed() { return closed; }
public boolean earnedReward() { return earned; }
public void run(){
if( _rewardedAd != null ){
Log.d(TAG, "BBAdmobRewarded run() -> _rewardedAd.show");
_rewardedAd.show(_activity,
new OnUserEarnedRewardListener() {
@Override
public void onUserEarnedReward(@NonNull RewardItem rewardItem) {
// Handle the reward.
Log.d(TAG, "The user earned the reward.");
int rewardAmount = rewardItem.getAmount();
String rewardType = rewardItem.getType();
earned = true;
}
});
return;
} else {
Log.e(TAG, "BBAdmobRewarded run() -> _rewardedAd = null");
loaded = false;
earned = false;
closed = false;
}
// load the first ad
loadRewardedAd();
}
// start the thread
private void startAd(String adUnitId){
Log.d(TAG, "BBAdmobRewarded startAd()");
this.adUnitId = adUnitId;
BBAndroidGame.AndroidGame().GetGameView().post(this);
}
private void loadRewardedAd() {
if (_rewardedAd == null) {
loaded = false;
Log.d(TAG, "BBAdmobRewarded adRequest");
AdRequest adRequest = new AdRequest.Builder().build();
RewardedAd.load(
_activity,
this.adUnitId,
adRequest,
new RewardedAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull RewardedAd rewardedAd) {
Log.d(TAG, "BBAdmobRewarded onAdLoaded");
loaded=true;
_rewardedAd = rewardedAd;
_rewardedAd.setFullScreenContentCallback(new FullScreenContentCallback()
{
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
//mInterstitialAd = null;
Log.d(TAG, "BBAdmobRewarded FullScreenContentCallback -> The ad was dismissed.");
closed = true;
_rewardedAd = null;
_admob.loadRewardedAd();
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d(TAG, "BBAdmobRewarded FullScreenContentCallback -> The ad failed to show.");
_rewardedAd = null;
closed = true;
earned = false;
loaded = false;
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
_rewardedAd = null;
earned = false;
closed=false;
Log.d(TAG, "BBAdmobRewarded FullScreenContentCallback -> The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error.
Log.d(TAG, loadAdError.getMessage());
_rewardedAd = null;
}
});
}
}
// displays the ad to the user if it is ready
public void ShowAdViewRewarded( ){
if (_rewardedAd != null ) {
if( loaded ){
Log.d(TAG, "BBAdmobRewarded ShowAdViewRewarded() -> loaded = true");
BBAndroidGame.AndroidGame().GetGameView().post(this);
} else {
Log.e(TAG, "BBAdmobRewarded ShowAdViewRewarded() -> loaded = false");
}
} else {
Log.e(TAG, "BBAdmobRewarded ShowAdViewRewarded() -> _rewardedAd = null");
loaded = false;
earned = false;
closed = false;
}
}
}
Yeah. I tried here without connection, so I changed the code a bit to 'load' another ad if it failed. But if the connection is off, it will keep reloading forever. I'm still undecided on what would be the best approach for this.* Don't know what will happen if an ad fail to load.
It gives you black screen when offline.
There is! admobRewarded.earnedReward() - the test program should print on the debug window a "yay! reward earned!"there should be a flag to tel the game what happened to the ad
That's controlled by Google, not me. When you display the ad, its another view on top of yours, controlled by Admob.I now also wonder how to make it possible for the user to cancel whenever
#REM
SETUP NEEDED:
STEP 1
Open [project build]\android\gradletemplate\app\src\main\AndroidManifest.xml and edit
just before ${ANDROID_MANIFEST_APPLICATION} :
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="real_app_id_generated_by_admob_here"/>
The value to place on 'real_app_id_generated_by_admob_here'' is given by Admob, when you create an app/ad over there. You can find it following this tutorial: https://support.google.com/admob/answer/7356431?hl=en
STEP 2
Open [project build]/android/gradletemplate/build.gradle
replace every
maven {
url "https://maven.google.com"
}
with
mavenCentral()
#End
Import mojo2
#If TARGET="android"
#If ANDROID_LIBGOOGLEPLAY_AVAILABLE
Import brl.admob
#End
Import "native/rewardedads.java"
#End If
Extern
Class AdmobRewarded Extends Null = "BBAdmobRewarded"
Function GetAdmobRewarded:AdmobRewarded(adUnitId:String)
Method ShowAdViewRewarded:Void()
Method isLoaded:Bool() ' was the ad loaded?
Method earnedReward:Bool() ' did the user earned the reward?
Method isClosed:Bool() ' was the ad closed?
End Class
Public
#If TARGET="android"
#ANDROID_APP_LABEL="Rewarded ads"
#ANDROID_APP_PACKAGE="com.icongames.rewardedAdsTest"
#ANDROID_SCREEN_ORIENTATION="sensorPortrait" '"user", "portrait", "landscape"
#ANDROID_VERSION_CODE="001"
#ANDROID_VERSION_NAME="0.01"
' test ads
#ADMOB_PUBLISHER_ID = "ca-app-pub-3940256099942544/5224354917"
#End
Class Game Extends App
Global IDLE:Int = 0
Global AD_VISIBLE:Int = 1
Global AD_CLOSED:Int = 2
Global UPDATE_RATE:Int = 30
Field canvas:Canvas
Field state:Int
Field admobRewarded:AdmobRewarded
Method OnCreate()
#If TARGET="android"
' test ads
admobRewarded = AdmobRewarded.GetAdmobRewarded("ca-app-pub-3940256099942544/5224354917")
#End
canvas = New Canvas()
SetSwapInterval 1 ; SetUpdateRate 0
' SetUpdateRate UPDATE_RATE
state = IDLE
End Method
Method OnUpdate()
Select state
Case IDLE
If TouchHit(0) And admobRewarded.isLoaded() Then
Print "Showing rewarded ad..."
admobRewarded.ShowAdViewRewarded()
state = AD_VISIBLE
End If
Case AD_VISIBLE
If admobRewarded.isClosed() Then
state = AD_CLOSED
End If
Case AD_CLOSED
If admobRewarded.earnedReward() Then
Print "Yay! Reward earned"
Else
Print "Sorry, no reward."
End If
state = IDLE
End Select
End Method
Method OnRender()
canvas.Clear 0,0,0
Select state
Case IDLE
If admobRewarded.isLoaded() Then
canvas.SetColor 1,1,1
canvas.DrawText "Touch the screen to open the ad!", 10,10
End If
Case AD_VISIBLE
Case AD_CLOSED
End Select
canvas.Flush
End Method
End Class
Function Main()
New Game()
End Function