FMX.Advertising.TBannerAd (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example shows how to add an advertising banner to a mobile application.

To build and test this example:

  1. Create a blank Multi-Device Application.
  2. Add a TBannerAd and a TLabel to your main form. You can place the banner on top and the label right below.
  3. For Android, configure the connection data for your advertising service.
  4. Set the TestMode property of your TBannerAd to True.
  5. Define the following string for the label text: "Click the ad above!".
  6. In "Unit1.pas", in the private section of your form class, declare:
    ActionBeginDate: TDateTime;
    WastedSeconds: Integer;
  7. In the uses section, add the "System.DateUtils" unit.
  8. Define event handlers with the implementations shown in the Code section below.
    1. Select the form and define an event handler for its OnShow event.
    2. Select the banner and define event handlers for its OnActionCanBegin and OnActionDidFinish events.

FMX.Advertising.TBannerAd.png

Code

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,
  FMX.Advertising, System.DateUtils;

type
  TForm1 = class(TForm)
    BannerAd1: TBannerAd;
    Label1: TLabel;
    procedure FormShow(Sender: TObject);
    procedure BannerAd1ActionCanBegin(Sender: TObject;
      var WillLeaveApplication: Boolean);
    procedure BannerAd1ActionDidFinish(Sender: TObject);
  private
    ActionBeginDate: TDateTime;
    WastedSeconds: Integer;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.BannerAd1ActionCanBegin(Sender: TObject;
  var WillLeaveApplication: Boolean);
begin
  ActionBeginDate := Now;
end;

procedure TForm1.BannerAd1ActionDidFinish(Sender: TObject);
var
  Seconds: Integer;
begin
  Seconds := SecondsBetween(ActionBeginDate, Now);
  WastedSeconds := WastedSeconds + Seconds;
  Label1.Text := IntToStr(WastedSeconds) + ' seconds wasted watching ads so far.'
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  BannerAd1.LoadAd;
end;

end.

Uses

See Also