Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey, Delphi Android, Delphi IOS

Launch Activities And Catch The Result In Delphi XE6 Firemonkey On Android

| Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOS

Delphi XE6 Firemonkey Android Activity Result | Delphi 11 10 XE8 XE7 XE Seattle Berlin Tokyo Rio Firemonkey Delphi Android Delphi IOSBrian Long has a new comprehensive article up which covers launching activities and returning the result in Delphi XE6 Firemonkey on Android. Delphi XE6 has expanded functionality to get the results returned from an activity which was much more difficult to do in Delphi XE5. In addition to the activity results the article demonstrates how to show an Android Toast, show the Android battery usage screen, launch a URL, send an SMS, send an email, and launch a QR Scanner. The most important part of the article is the code demonstrating how to get results from the launched activity. Previously with the QR Scanner for example the tricky way to do it was via the Clipboard. Now with Delphi XE6 Firemonkey you can actually return and process the results of the startActivityForResult() Android API call. Here is a sample of the source code that starts the activity and then processes the results:
procedure TMainForm.BarcodeScannerButtonClick(Sender: TObject);
begin
FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification,
HandleActivityMessage);
LaunchQRScanner(ScanRequestCode);
end;

procedure TMainForm.HandleActivityMessage(const Sender: TObject; const M: TMessage);
begin
if M is TMessageResultNotification then
OnActivityResult(TMessageResultNotification(M).RequestCode, TMessageResultNotification(M).ResultCode,
TMessageResultNotification(M).Value);
end;

function TMainForm.OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
var
ScanContent, ScanFormat: string;
begin
Result := False;

TMessageManager.DefaultManager.Unsubscribe(TMessageResultNotification, FMessageSubscriptionID);
FMessageSubscriptionID := 0;

// For more info see https://github.com/zxing/zxing/wiki/Scanning-Via-Intent
if RequestCode = ScanRequestCode then
begin
if ResultCode = TJActivity.JavaClass.RESULT_OK then
begin
if Assigned(Data) then
begin
ScanContent := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT')));
ScanFormat := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT_FORMAT')));
Toast(Format('Found %s format barcode:'#10'%s', [ScanFormat, ScanContent]), LongToast);
end;
end
else if ResultCode = TJActivity.JavaClass.RESULT_CANCELED then
begin
Toast('You cancelled the scan', ShortToast);
end;
Result := True;
end;
end;
At the top of the article there is also a link to a similar article for Delphi XE5.

Head over and read the full article about starting an Activity on Android and processing the results with Delphi XE6.

Exit mobile version