FMX.Advertising.TBannerAd (Delphi)
Contents
Description
This example shows how to add an advertising banner to a mobile application.
To build and test this example:
- Create a blank Multi-Device Application.
- Add a TBannerAd and a TLabel to your main form. You can place the banner on top and the label right below.
- For Android, configure the connection data for your advertising service.
- Set the TestMode property of your TBannerAd to
True
. - Define the following string for the label text: "Click the ad above!".
- In "Unit1.pas", in the
private
section of your form class, declare:ActionBeginDate: TDateTime;
WastedSeconds: Integer;
- In the
uses
section, add the "System.DateUtils" unit. - Define event handlers with the implementations shown in the Code section below.
- Select the form and define an event handler for its OnShow event.
- Select the banner and define event handlers for its OnActionCanBegin and OnActionDidFinish events.
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
- FMX.Advertising.TBannerAd ( fr | de | ja )
- System.DateUtils.SecondsBetween ( fr | de | ja )