-
Notifications
You must be signed in to change notification settings - Fork 6
/
FMX.InterstitialAd.Android.pas
191 lines (150 loc) · 5.06 KB
/
FMX.InterstitialAd.Android.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
{
Author : Ersan YAKIT
ersanyakit@yahoo.com.tr
www.ersanyakit.com
Admob Interstitials Snippet Code Using The Android JNI In Delphi XE7 Firemonkey
}
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,
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;
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
AInterstitialAdvertisment.FEventOnAdClosed('onAdClosed:Ok');
end;
procedure TInterstitialAdListener.onAdFailedToLoad(errorCode: Integer);
begin
AInterstitialAdvertisment.FEventonAdOpened('onAdFailedToLoad:OK:ErrorCode:'+IntToStr(errorCode));
end;
procedure TInterstitialAdListener.onAdLeftApplication;
begin
AInterstitialAdvertisment.FEventonAdOpened('onAdLeftApplication:OK');
end;
procedure TInterstitialAdListener.onAdOpened;
begin
AInterstitialAdvertisment.FEventonAdOpened('onAdOpened:OK');
end;
procedure TInterstitialAdListener.onAdLoaded;
begin
AInterstitialAdvertisment.FEventononAdLoaded('onAdLoaded:OK');
AInterstitialAdvertisment.FJInterstitialAd.show;
end;
//Component;
constructor TInterstitialAdvertisment.Create;
begin
inherited Create;
FTestMode := True;
AInterstitialAdvertisment:=Self;
end;
procedure TInterstitialAdvertisment.setAdUnitId(AUnitID : string);
begin
if AUnitID<>FAdUnitID then FAdUnitID := AUnitID;
end;
procedure TInterstitialAdvertisment.InitAdvertisment;
begin
CallInUIThread(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.