Jump to content
Sign in to follow this  
Yaron

Android AdMob interstitial ads

Recommended Posts

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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×