Yaron 53 Posted August 9, 2020 I got a DM with a request on how I added Android AdMob interstitial ads, here's the code: First the definition: {$IF DEFINED(ANDROID) and DEFINED(ADMOB_FULLPAGE)} TInterStitialAdViewListener = class(TJavaLocal, JIAdListener) private FAD: JInterstitialAd; public constructor Create(AAD: JInterstitialAd); procedure onAdClosed; cdecl; procedure onAdFailedToLoad(errorCode: Integer); cdecl; procedure onAdLeftApplication; cdecl; procedure onAdOpened; cdecl; procedure onAdLoaded; cdecl; end; {$ENDIF} [code] Then in the private section of the main form: [code] {$IF DEFINED(ANDROID) and DEFINED(ADMOB_FULLPAGE)} LAdViewListener : TInterStitialAdViewListener; FInterStitial : JInterstitialAd; {$ENDIF} In the form's onCreate: {$IFDEF ADMOB_FULLPAGE} FInterStitial := TJInterstitialAd.JavaClass.init(MainActivity); {$IFDEF TRACEDEBUG} FInterStitial.setAdUnitId(StringToJString('ca-app-pub-3940256099942544/1033173712')); // google test ad {$ELSE} FInterStitial.setAdUnitId(StringToJString('ca-app-pub-xxxxxxxxxxxxxxxxxxx/xxxxxxxxxxx')); // real ad code {$ENDIF} {$ENDIF} And finally: {$IF DEFINED(ANDROID) and DEFINED(ADMOB_FULLPAGE)} constructor TInterStitialAdViewListener.Create(AAD: JInterstitialAd); begin inherited Create; FAD := AAD; {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener created');{$ENDIF} end; procedure TInterStitialAdViewListener.onAdClosed; begin {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener AdClosed event');{$ENDIF} //MainForm.ShowModal; end; procedure TInterStitialAdViewListener.onAdFailedToLoad(errorCode: Integer); begin {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener AdFailedToLoad code #'+errorCode.toString);{$ENDIF} {$IFDEF TRACEDEBUG}ShowMessage('AdFailedToLoad code #'+errorCode.toString);{$ENDIF} end; procedure TInterStitialAdViewListener.onAdLeftApplication; begin {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener AdLeftApplication');{$ENDIF} end; procedure TInterStitialAdViewListener.onAdLoaded; begin {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener AdLoaded');{$ENDIF} FAD.show; end; procedure TInterStitialAdViewListener.onAdOpened; begin {$IFDEF TRACEDEBUG}AddDebugEntry('InterStitialAdViewListener AdOpened');{$ENDIF} end; procedure TMainForm.ShowInterStitialAd; var LADRequestBuilder: JAdRequest_Builder; LadRequest: JAdRequest; begin LADRequestBuilder := TJAdRequest_Builder.Create; {$IFDEF TRACEDEBUG}LADRequestBuilder.addTestDevice(MainActivity.getDeviceID);{$ENDIF} LadRequest := LADRequestBuilder.build(); LAdViewListener := TInterStitialAdViewListener.Create(FInterStitial); CallInUIThread( procedure begin FInterStitial.setAdListener(TJAdListenerAdapter.JavaClass.init (LAdViewListener)); FInterStitial.loadAd(LadRequest); end); end; {$ENDIF} Share this post Link to post
okba-dz 1 Posted January 3, 2022 Great if it was a little organized but thanks anyway Share this post Link to post
Edson Vasconcelos 0 Posted October 15, 2022 (edited) Da erro nessa linha no delphi 11, como resolver?? FAD: JInterstitialAd; Edited October 15, 2022 by Edson Vasconcelos Share this post Link to post