Android Tutorial FirebaseAdMob - Rewarded video ads

Erel

B4X founder
Staff member
Licensed User
Longtime User
You can use this code to load an ad with a test device (instead of calling LoadAd);
B4X:
Dim builder As JavaObject
builder.InitializeNewInstance("com.google.android.gms.ads.AdRequest.Builder", Null)
builder.RunMethod("addTestDevice", Array(Test device string here))
Dim jo As JavaObject = RewardedVideoAd1
jo.RunMethod("loadAd", Array(builder.RunMethod("build", Null))
 

alimanam3386

Active Member
Licensed User
Longtime User

Thank you Erel, Can I pass any string variable for test device string right ?
 

Feten7

Member
Licensed User
Hi,

What i don't understand is why in some devices it keeps restarting the activity and sometimes the whole app. I don't want to put the app on Play Store until i'm sure it's going to work. Here are the logs

And here the code
B4X:
Sub Globals
    'These global variables will be redeclared each time the activity is created.
    'These variables can only be accessed from this module.
    Private ad As RewardedVideoAd
    Dim dato As RandomAccessFile
    Dim fecha As Long
    Dim intentos As Int
End Sub

Sub Activity_Create(FirstTime As Boolean)
    'Do not forget to load the layout file created with the visual designer. For example:
    'Activity.LoadLayout("Layout1")
    ad.Initialize("ad")
    intentos=0


End Sub

Sub Activity_Resume
    ad.LoadAd("ca-app-pub-9543716757242777/7172194814")
    Msgbox2Async("Ici, après d'avoir vu un petit vidéo, vous aurez l'app libre de publicité pour un mois. Voulez vous continuer?","Se débarraser de la publicité","Oui","","Non",Null,False)
    Wait For MsgBox_Result (result As Int)
    If result = DialogResponse.NEGATIVE Then
        Activity.Finish
    End If
End Sub
Sub ad_ReceiveAd
    ad.Show
End Sub
Sub Ad_Rewarded (Item As Object)
    fecha=DateTime.Now
    File.OpenOutput(File.DirDefaultExternal,"data.txt",False)
    dato.Initialize(File.DirDefaultExternal,"data.txt",False)

    dato.WriteLong(fecha,0)
    dato.Close
    Msgbox("Vous avez gagné un mois sans publicité! Merci","Fini")
    StartActivity(Main)
End Sub

Sub Ad_FailedToReceiveAd (ErrorCode As String)
    If intentos<30 Then
    Log("Failed: " & ErrorCode)
    ad.LoadAd("ca-app-pub-9543716757242777/7172194814")
    intentos=intentos+1
    Else
    Msgbox("Il y a quelques problèmes","Problèmes")
    Activity.Finish
    End If
End Sub

Sub Ad_AdClosed

End Sub

Sub Ad_AdOpened

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Can someone help me?

Thanks
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Thank you Erel, Can I pass any string variable for test device string right ?
The test device string should be printed in the logs.

Can someone help me?
You aren't calling ExitApplicaton somewhere, right?

Try to call Service.StartForeground in the starter service before you show the ad.
 

Feten7

Member
Licensed User
Hi,

You aren't calling ExitApplicaton somewhere, right?

No, i'm not calling ExitApplication.

Try to call Service.StartForeground in the starter service before you show the ad.

Sorry but i don't understand, must i create a service in order to show the ad?
 

jchal

Active Member
Licensed User
Longtime User
i tryed to run the example but i get error code 0 what does that means , i dont see any adds at all
 

alimanam3386

Active Member
Licensed User
Longtime User
Hi

When I use the Erel code in post1 of this topic I get this error:

B4X:
sending message to waiting queue (ad_adopened)
java.lang.AbstractMethodError: abstract method "void com.google.android.gms.ads.reward.RewardedVideoAdListener.onRewardedVideoCompleted()"
    at com.google.android.gms.internal.ads.zzahj.onRewardedVideoCompleted(Unknown Source)
    at com.google.android.gms.internal.ads.zzahf.dispatchTransaction(Unknown Source)
    at com.google.android.gms.internal.ads.zzek.onTransact(Unknown Source)
    at android.os.Binder.transact(Binder.java:387)
    at hl.b(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):20)
    at com.google.android.gms.ads.internal.reward.client.l.f(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):27)
    at com.google.android.gms.ads.internal.a.u(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):471)
    at com.google.android.gms.ads.internal.reward.d.f_(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):87)
    at com.google.android.gms.ads.internal.reward.mediation.h.h(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):37)
    at com.google.android.gms.ads.internal.reward.client.n.f(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):96)
    at com.google.android.gms.ads.internal.a.u(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):471)
    at com.google.android.gms.ads.internal.ak.Z(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):277)
    at com.google.android.gms.ads.internal.gmsg.am.a(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):28)
    at com.google.android.gms.ads.internal.webview.j.b(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):356)
    at com.google.android.gms.ads.internal.webview.ab.run(Unknown Source)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at com.google.android.gms.ads.internal.util.f.dispatchMessage(:com.google.android.gms.dynamite_dynamitemodulesa@12529017@12.5.29 (040300-192802242):9)
    at android.os.Looper.loop(Looper.java:158)
    at android.app.ActivityThread.main(ActivityThread.java:7225)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

TargetSdk in manifest set to 26 , and I tested this code on Samsung note4 , What's wrong ?
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…