Messagedlg in Delphi xe7 android - android

I m just trying to execute a sample given during the installation of Delphi xe7, the MessageAlerts on android platform, unfortunately it does not working, it gives the following error message:
Blocking Dialogs not implemented in this platform
procedure TMessageAlertsForm.btnMultiButtonAlertClick(Sender: TObject);
begin
{ Show a multiple-button alert that triggers different code blocks according to
your input }
case MessageDlg('Choose a button:', System.UITypes.TMsgDlgType.mtInformation,
[
System.UITypes.TMsgDlgBtn.mbYes,
System.UITypes.TMsgDlgBtn.mbNo,
System.UITypes.TMsgDlgBtn.mbCancel
], 0) of
{ Detect which button was pushed and show a different message }
mrYES:
ShowMessage('You chose Yes');
mrNo:
ShowMessage('You chose No');
mrCancel:
ShowMessage('You chose Cancel');
end;
end;
Any idea How to solve it?

This is explained in the XE7 release notes:
Dialog Box Methods Support Anonymous Methods to Handle Their Closing
In XE6, calls to dialog box methods (InputBox, InputQuery, MessageDlg, ShowMessage) were always blocking. Any code after a call to one of these methods is not executed until the dialog box closes. Android does not allow blocking dialog boxes, so you could not use these methods on Android.
On XE7, InputBox, InputQuery, and MessageDlg support a new optional parameter, ACloseDialogProc. Calls that include this new parameter work on all platforms, including Android. This new optional parameter allows you to provide an anonymous method that is called when the dialog box closes. When you call these methods using this new parameter, your call is blocking in desktop platforms and non-blocking in mobile platforms. If you need to execute code after your dialog box closes, use this new parameter to ensure that your application works as expected on all supported platforms.
...
ShowMessage also gained support for Android in XE7, and calls to ShowMessage are blocking on desktop platforms and non-blocking on mobile platforms. However, ShowMessage does not provide any new parameter to handle its closing. If you need to execute code after the dialog box that ShowMessage shows closes, use MessageDlg instead of ShowMessage.
For example:
procedure TMessageAlertsForm.btnMultiButtonAlertClick(Sender: TObject);
begin
MessageDlg('Choose a button:', System.UITypes.TMsgDlgType.mtInformation,
[
System.UITypes.TMsgDlgBtn.mbYes,
System.UITypes.TMsgDlgBtn.mbNo,
System.UITypes.TMsgDlgBtn.mbCancel
], 0,
procedure(const AResult: System.UITypes.TModalResult)
begin
case AResult of
mrYES:
ShowMessage('You chose Yes');
mrNo:
ShowMessage('You chose No');
mrCancel:
ShowMessage('You chose Cancel');
end;
end);
end;
end;

Related

Android Test App perform programming steps backwards

I am using Delphi Enterprise version 10.2.3 with Android SDK ver 24.3.3 32bit. I tried a very simple program with only one button. The Onclick is simply the following:
ShowMessage('1');
ShowMessage('2');
ShowMessage('3');
ShowMessage('4');
The result I got on my Samsung phone when clicking on the button is:
4
3
2
1
Of course I am expecting to get
1
2
3
4
This is not my first Android Program. The previous ones runs smoothly. But when I got strange errors on my latest program, I found that programming steps are carried out in reverse. I am also scared now to recompile the previous apps, just in case I am getting this strange behavior. So I just make a new program (above) to test, but got the same results. I also disabled the Antivirus Avast program, and even try it on another Samsung device.
Help will be very much appreciated. At this moment I am really confused and not sure what next steps to take to solve the problem. Please help me!
On mobile platforms, ShowMessage behaves asynchronously. The call finishes instantaneously, it does not wait for the user to close the dialog box.
Try this code:
function TForm1.MyShowMessage(const Msg: String): TModalResult;
var
MR: TModalResult;
begin
MR := mrNone;
TDialogService.MessageDialog(Msg, TMsgDlgType.mtConfirmation,const [TMsgDlgBtn.mbYes, TMsgDlgBtn.mbNo], TMsgDlgBtn.mbYes, 0,
procedure(const AResult: TModalResult)
begin
 MR := AResult;
end);
while MR = mrNone do begin
Application.ProcessMessages;
CheckSynchronize;
end;
Result := MR;
end;

How to get app resume state on iOS and Android?

Is there something that can be checked from code point of view when an App is resumed on iOS and Android?
e.g. when an app gets minimized and restored (app is still running in background of device).
You need to use IFMXApplicationEventService to register a callback where the application will be notified:
uses FMX.Types, FMX.Platform;
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
begin
case AAppEvent of
TApplicationEvent.FinishedLaunching: Log.d('Launched.');
TApplicationEvent.BecameActive: Log.d('Gained focus.');
TApplicationEvent.EnteredBackground: Log.d('Now running in background.');
TApplicationEvent.WillBecomeForeground: Log.d('Restoring from background.');
TApplicationEvent.WillBecomeInactive: Log.d('Going to lose focus.');
TApplicationEvent.WillTerminate: Log.d('Quitting the application.');
TApplicationEvent.LowMemory: Log.d('Device running out of memory.');
// iOS only
TApplicationEvent.TimeChange: Log.d('Significant change in time.');
TApplicationEvent.OpenURL: Log.d('Request to open an URL.');
end;
Result := True;
end;
procedure TForm11.FormCreate(Sender: TObject);
var
aFMXApplicationEventService: IFMXApplicationEventService;
begin
if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService,
IInterface(aFMXApplicationEventService))
then
aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
else
Log.d('Application Event Service not supported.');
end;
More info about the event types here.
A good article on the subject by Paweł Głowacki (for Delphi XE5, but still useful).
In iOS You can add flag in
applicationDidEnterBackground
in appDelegate to know if the user enters the background and,
applicationDidBecomeActive
to know that the user returns to the app from background

Delphi Seattle cannot reference default FMX class

While trying to implement Notifications at my project, Delphi Seattle can't reference FMX.Notification properly.
This is what I get:
[DCC Fatal Error] UnitMain.pas(27): F2613 Unit 'FMX.Notification' not found.
And then it makes an automatically reference to System.Notification, however it crashes my Android app when trying to use a object from this class.
How can I correctly implement Notifications on Delphi Seattle?
Note: It must run on both iOS and Android.
According to Embarcadero's official Seattle changes:
The FMX.Notification unit has been replaced by System.Notification.
The TNotificationCenter component now supports Windows 8 and later Windows versions. This component has also undergone some minor changes:
It provides a Loaded property to check whether the notification center is ready to use or not.
The type of ApplicationIconBadgeNumber has changed from Word to Integer.
Its Supported method is no longer necessary and has been removed.
The TBaseNotificationCenter class has replaced the IFMXNotificationCenter interface. Classes that used to implement the IFMXNotificationCenter interface must become subclasses of TBaseNotificationCenter and implement the virtual abstract methods of their parent class.
Hereby how I figured out to display notifications now:
procedure TForm_Master.showNotification(Sender: TObject);
var
MyNotification: TNotification;
begin
MyNotification := NotificationCenter1.CreateNotification;
try
MyNotification.Name := 'NotificationName';
MyNotification.AlertBody :=
'Here goes your message';
MyNotification.FireDate := Now;
// Send notification to the notification center
NotificationCenter1.ScheduleNotification(MyNotification);
finally
MyNotification.Free;
end;
end;

Barcode reading Delphi xe7, event after intent not triggering

In my app, developed with XE7 for Android/iOS, I have a form for scanning barcodes. Upon a found barcode, my app validates whether it is an acceptable barcode or not. Following tutorials here: http://www.fmxexpress.com/qr-code-scanner-source-code-for-delphi-xe5-firemonkey-on-android-and-ios/
Currently I am testing on Android and I am able to integrate scanning and reading of barcodes, but the 'onBarCode' event does not fire when returned from the shared Activity of finding the barcode. Same code worked well with previous versions of Rad Studio ( XE4, XE5, XE6) but now in XE7 it does not.
Here are some snippets of code:
...
begin
Scanner := TAndroidBarcodeScanner.Create(true);
Scanner.OnBarCode := BarcodeHandler;
Scanner.Scan;
end;
procedure TmScannerForm.BarcodeHandler(Sender: TAndroidBarcodeScanner;
BarCode: String);
begin
text1.Text := Barcode;
memo1.PasteFromClipboard;
AddBarcode(BarCode, true);
end;
AddBarCode is the even I used to validate and add barcode to a list, but I didnt include it, because that code isn't the problem - it's not even triggering. The Text1.text:=Barcode and memo1.paseFromClipboard were in their for validating the even wasn't firing too. I can confirm the barcodes are being read because if I tap and manually paste, the barcode shows.
Why is this not working on XE7 as it did in previous versions of Rad Studio?
Andrea Magni has a more elegant solution than the timer on his blog based on event handling.
I would comment to send the link but I don't have enough reputation.
The link to his blog is :
http://blog.delphiedintorni.it/2014/10/leggere-e-produrre-barcode-con-delphi.html
Maybe this can help you. The blog is in Italian though but the sources are provided and are explaining by themselves.
There is a source code fragment on http://john.whitham.me.uk/xe5/ which looks useable (based on Zxing):
intent := tjintent.Create;
intent.setAction(stringtojstring('com.google.zxing.client.android.SCAN'));
sharedactivity.startActivityForResult(intent,0);
The code in the linked article also shows how to receive the Intent result. (I don't work with Delphi on Android so I am not sure if that part uses a best practice - TTKRBarCodeScanner uses a workaround with a Timer and the Clipboard).
I would try this as an alternative to see if works.
This code works to me!
Set timer enabled to true when you run your scan code
procedure Tform.Timer1Timer(Sender: TObject);
begin
if (ClipService.GetClipboard.ToString <> '') then
begin
timer1.Enabled:=false;
zSearch.Text := ClipService.GetClipboard.ToString;
//Do what you need
end;
end;
This code to me works fine!
in andorid.BarcodeScanner
function TAndroidBarcodeScanner.HandleAppEvent(AAppEvent: TApplicationEvent;
AContext: TObject): Boolean;
var
aeBecameActive : TApplicationEvent;
begin
aeBecameActive := TApplicationEvent.BecameActive;
if FMonitorClipboard and (AAppEvent = aeBecameActive) then
begin
GetBarcodeValue;
end;
end;

Delphi XE5 Anonymous ShowModal doesn't work as expected

I am a newbie to Delphi XE5 and currently developing Android platform applications on my Windows desktop using Delphi XE5.
I have two forms(Form1 and Form2) and tried to show Form2 in modal way on Form1 according to the way showed in Marco's RAD Blog(http://blog.marcocantu.com/blog/xe5_anonymous_showmodal_android.html).
But result was not as expected.
procedure TForm1.Button1Click(Sender: TObject);
var
frm2: TForm2;
begin
frm2 := TForm2.Create(nil);
ShowMessage('before frm2.ShowModal...');
frm2.ShowModal (
procedure(ModalResult: TModalResult)
begin
if ModalResult = mrOK then
if frm2.ListBox1.ItemIndex >= 0 then
edit1.Text := frm2.ListBox1.Items [frm2.ListBox1.ItemIndex];
frm2.DisposeOf;
end
);
ShowMessage('after frm2.ShowModal...');
end;
I wrote above code and run the application on an Android device.
I clicked the Button1, then I got the messagebox "before frm2.ShowModal... ", next "after frm2.ShowModal...", and then Form2 was showed.
I expect that the order should be 1)"before frm2.ShowModal... " message, 2) Form2 being showed, and 3) "after frm2.ShowModal..." message.
What's wrong with me?
The call to the anonymous ShowModal is not blocking, which means that any code after the ShowModal will be executed first.
One note here. Calling frm2.DisposeOf is wrong.
You must use this pattern:
declare
procedure TFrm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin
Action := TCloseAction.caFree;
end;
See http://www.malcolmgroves.com/blog/?p=1585.
The documentation has been corrected in XE7, Using FireMonkey Modal Dialog Boxes, but this pattern can be used in all Delphi versions.
Conclusion: if you want to execute code after the modal dialog is closed, put that code inside the anonymous method.

Categories

Resources