// Düzeltme, Ardışık tıklamalarda programin crash olmasına sebebiyet veren sorunların düzeltildiği sürüm...
Çalışan Proje :

http://ersanyakit.com/Interstitial.rar


Unite Kodu
{
  Author : Ersan YAKIT
           ersanyakit@yahoo.com.tr
           www.ersanyakit.com
           credits goes to Saadettin POLAT!

}
unit FMX.InterstitialAd.Android;

interface

uses
  System.SysUtils,
  System.Types,
  System.UITypes,
  System.Classes,
  System.Variants,
  FMX.Types,
  FMX.Controls,
  FMX.Forms,
  FMX.Graphics,
  FMX.Dialogs,
  FMX.Platform,
  Androidapi.JNI.AdMob,
  Androidapi.JNIBridge,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Widget,
  Androidapi.JNI.Location,
  Androidapi.JNI.App,
  Androidapi.JNI.Util,
  Androidapi.helpers,
  FMX.helpers.android,
  FMX.Platform.Android,
  Androidapi.JNI.Embarcadero,
  Androidapi.JNI.GraphicsContentViewText,
  FMX.Advertising,
  FMX.StdCtrls ;

type
  TInterstitialAdListener = class(TJavaLocal, JIAdListener)
  private
    FAd : JInterstitialAd;
  public
    procedure onAdClosed; cdecl;
    procedure onAdFailedToLoad(errorCode: Integer); cdecl;
    procedure onAdLeftApplication; cdecl;
    procedure onAdOpened; cdecl;
    procedure onAdLoaded; cdecl;
  end;


  TEventProc          = procedure(pszData : string);

 type
  TInterstitialAdvertisment = class
  private
      FTestMode : Boolean;
      FScale : Single;
      procedure SetTestMode(AMode:Boolean);
  public
      FJAdRequest      : JAdRequest;
      FJInterstitialAd : JInterstitialAd;
      RequestBuilder   : JAdRequest_Builder;
      FAdUnitID        : string;
      FInterstitialAdListener : TInterstitialAdListener;

      FEventOnAdClosed          : TEventProc;
      FEventonAdFailedToLoad    : TEventProc;
      FEventonAdLeftApplication : TEventProc;
      FEventonAdOpened          : TEventProc;
      FEventononAdLoaded        : TEventProc;

      constructor Create;
      procedure SetOnCloseEvent(AProc : TEventProc);
      procedure SetOnAdFailedToLoad(AProc : TEventProc);
      procedure SetOnAdLeftApplication(AProc : TEventProc);
      procedure SetOnAdOpened(AProc : TEventProc);
      procedure SetOnAdLoaded(AProc : TEventProc);
      procedure InitAdvertisment;
      procedure setAdUnitId(AUnitID : string);

  property  TestMode : Boolean read FTestMode write SetTestMode;


  end;

 var
  AInterstitialAdvertisment : TInterstitialAdvertisment;


implementation


// Listener;
procedure TInterstitialAdListener.onAdClosed;
begin
  if Assigned(AInterstitialAdvertisment)
    then begin
           AInterstitialAdvertisment.FEventOnAdClosed('onAdClosed:Ok');
         end;
end;

procedure TInterstitialAdListener.onAdFailedToLoad(errorCode: Integer);
begin
  if Assigned(AInterstitialAdvertisment)
    then begin
             AInterstitialAdvertisment.FEventonAdOpened('onAdFailedToLoad:OK:ErrorCode:'+IntToStr(errorCode));
         end;
end;

procedure TInterstitialAdListener.onAdLeftApplication;
begin
  if Assigned(AInterstitialAdvertisment)
    then begin
            AInterstitialAdvertisment.FEventonAdOpened('onAdLeftApplication:OK');
         end;
end;

procedure TInterstitialAdListener.onAdOpened;
begin
  if Assigned(AInterstitialAdvertisment)
    then begin
            AInterstitialAdvertisment.FEventonAdOpened('onAdOpened:OK');
         end;
end;

procedure TInterstitialAdListener.onAdLoaded;
begin
  if Assigned(AInterstitialAdvertisment)
     then begin
              CallInUIThreadAndWaitFinishing(
                procedure
                begin
                    if Assigned(AInterstitialAdvertisment.FJInterstitialAd)
                      then begin
                            if AInterstitialAdvertisment.FJInterstitialAd.isLoaded
                              then begin
                                       AInterstitialAdvertisment.FJInterstitialAd.show;
                                   end;
                             AInterstitialAdvertisment.FEventononAdLoaded('onAdLoaded:OK');
                           end;
                end);
          end;
end;

//Component;
constructor TInterstitialAdvertisment.Create;
var
   ScreenService: IFMXScreenService;
begin
    inherited Create;
    FTestMode := True;

    if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, ScreenService) then
      FScale := ScreenService.GetScreenScale
    else
      FScale := 1;

    AInterstitialAdvertisment:=nil;
    FJInterstitialAd:=nil;
    RequestBuilder:=nil;
    FJAdRequest:=nil;

    FEventOnAdClosed          :=nil;
    FEventonAdFailedToLoad    :=nil;
    FEventonAdLeftApplication :=nil;
    FEventonAdOpened          :=nil;
    FEventononAdLoaded        :=nil;

   AInterstitialAdvertisment:=Self;
end;

procedure TInterstitialAdvertisment.setAdUnitId(AUnitID : string);
begin
  if AUnitID<>FAdUnitID then FAdUnitID := AUnitID;
end;

procedure TInterstitialAdvertisment.InitAdvertisment;
begin


 CallInUIThreadAndWaitFinishing(procedure
                  begin
                      FJInterstitialAd := TJInterstitialAd.JavaClass.init(MainActivity);


                      FJInterstitialAd.setAdUnitId(StringToJString(FAdUnitID));
                      RequestBuilder := TJAdRequest_Builder.JavaClass.init();

                      if FTestMode = true
                        then begin
                              RequestBuilder.addTestDevice(TJAdRequest.JavaClass.DEVICE_ID_EMULATOR);
                              RequestBuilder.addTestDevice(MainActivity.getDeviceID);
                             end;

                      FJAdRequest := RequestBuilder.build;
                      FInterstitialAdListener := TInterstitialAdListener.Create();

                      FJInterstitialAd.setAdListener(TJAdListenerAdapter.JavaClass.init(FInterstitialAdListener));
                      FJInterstitialAd.loadAd(FJAdRequest);
                  end);

end;

procedure TInterstitialAdvertisment.SetTestMode(AMode:Boolean);
begin
  FTestMode := AMode;
end;

procedure TInterstitialAdvertisment.SetOnCloseEvent(AProc : TEventProc);
begin
    FEventOnAdClosed := AProc;
end;

procedure TInterstitialAdvertisment.SetOnAdFailedToLoad(AProc : TEventProc);
begin
    FEventonAdFailedToLoad := AProc;
end;

procedure TInterstitialAdvertisment.SetOnAdLeftApplication(AProc : TEventProc);
begin
    FEventonAdLeftApplication := AProc;
end;

procedure TInterstitialAdvertisment.SetOnAdOpened(AProc : TEventProc);
begin
    FEventonAdOpened := AProc;
end;

procedure TInterstitialAdvertisment.SetOnAdLoaded(AProc : TEventProc);
begin
    FEventononAdLoaded := AProc;
end;

end.

Örnek Kullanım
type
  TForm1 = class(TForm)
    Button2: TButton;
    Memo1: TMemo;
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
          IAdvertisment : TInterstitialAdvertisment;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure onAdClosedEvent(pszData:String);
begin
  Form1.Memo1.Lines.add('onAdClosed');
end;

procedure onAdFailedToLoadEvent(pszData:String);
begin
  Form1.Memo1.Lines.add('onAdFailedToLoad');
end;

procedure onAdLeftApplicationEvent(pszData:String);
begin
  Form1.Memo1.Lines.add('onAdLeftApplication');
end;

procedure onAdOpenedEvent(pszData:String);
begin
  Form1.Memo1.Lines.add('onAdOpened');
end;

procedure onAdLoadedEvent(pszData:String);
begin
  Form1.Memo1.Lines.add('onAdLoaded');
end;


procedure TForm1.Button2Click(Sender: TObject);
begin
  IAdvertisment := TInterstitialAdvertisment.Create;
  IAdvertisment.SetOnCloseEvent(onAdClosedEvent);
  IAdvertisment.SetOnAdFailedToLoad(onAdFailedToLoadEvent);
  IAdvertisment.SetOnAdLeftApplication(onAdLeftApplicationEvent);
  IAdvertisment.SetOnAdOpened(onAdOpenedEvent);
  IAdvertisment.SetOnAdLoaded(onAdLoadedEvent);
  IAdvertisment.TestMode := True;
  IAdvertisment.SetAdUnitID('ca-app-pub-1543398996171627/8995432791');
  IAdvertisment.InitAdvertisment;
end;