Einzelnen Beitrag anzeigen

QuickAndDirty

Registriert seit: 13. Jan 2004
Ort: Hamm(Westf)
1.889 Beiträge
 
Delphi 12 Athens
 
#1

Intent für BarcodeScan geht unter XE8 aber nicht unter Berlin

  Alt 26. Jan 2017, 17:30
Hallo ich benutze folgenden Code aus dem Internet zum Scannen von Barcodes.
Seltsamer Weise kommt er nie in der Methode "HandleActivityMessage" an. Statt dessen scheint es so als würde meine App beendet und neugestartet.

Habt ihr eine Idee was man machen muss, damit das mit Delphi Berlin unter Android 4.1.2 läuft?


Delphi-Quellcode:
unit uBarcodeScannerIntent;

interface
  Uses
    FMX.platform,
  {$IFDEF ANDROID}
    fmx.helpers.android, androidapi.JNI.GraphicsContentViewText, androidapi.jni.JavaTypes,
    androidapi.helpers,androidapi.jni.App,
  {$ENDIF}
   classes,types,FMX.types, system.Messaging;

type

  TBarcodeScannerIntent = class(TComponent)
  private
    fbarcode: String;
    fOnBarcodeScanned: TNotifyevent;
    fOnScanCanceled: TNotifyevent;
    fCodeType: String;
    fBarcodeFormat: String;

    const SCAN_REQUEST_CODE = 0;
    var FMessageSubscriptionID: Integer;
    procedure HandleActivityMessage(const Sender: TObject; const M: TMessage);
    {$IFDEF ANDROID}
    function OnActivityResult(RequestCode, ResultCode: Integer; Data: JIntent): Boolean;
    {$ENDIF}

  public
    Constructor Create(aOwner : TComponent);Override;
    Procedure Scan;
    Property CodeType:String read fCodeType write fCodetype;
    property Barcode:String read fbarcode;
    property BarcodeFormat:String read fBarcodeFormat Write fBarcodeFormat;
    property OnBarcodeScanned:TNotifyevent read fOnBarcodeScanned write fOnbarcodescanned;
    property OnScanCanceled:TNotifyevent read fOnScanCanceled write fOnScanCanceled;

  end;

implementation

{ TBarcodeScannerIntent }

constructor TBarcodeScannerIntent.create(aOwner: TComponent);
begin
  inherited;
  fCodetype := '';
  fbarcode := '';
  fBarcodeFormat := '';
end;


procedure TBarcodeScannerIntent.HandleActivityMessage(const Sender: TObject;
  const M: TMessage);
begin
{$IFDEF ANDROID}
 if M is TMessageResultNotification then
    OnActivityResult(
      TMessageResultNotification(M).RequestCode,
      TMessageResultNotification(M).ResultCode,
      TMessageResultNotification(M).Value);
 {$ENDIF}
end;

{$IFDEF ANDROID}
function TBarcodeScannerIntent.OnActivityResult(RequestCode,
  ResultCode: Integer; Data: JIntent): Boolean;
var
  LScanContent, LScanFormat: string;
begin
  Result := False;

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

  if RequestCode = SCAN_REQUEST_CODE then
  begin
    if ResultCode = TJActivity.JavaClass.RESULT_OK then
    begin
      Result := True;

      if Assigned(Data) then
      begin
        LScanContent := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT')));
        LScanFormat := JStringToString(Data.getStringExtra(StringToJString('SCAN_RESULT_FORMAT')));
        TThread.Synchronize(nil,
          procedure
          begin
            fbarcode := LScanContent;
            fBarcodeFormat := LScanFormat;
            if assigned(fOnBarcodeScanned) then
              fonbarcodescanned(self);
          end
        );
      end;
    end
    else
    Begin
      fbarcode := '';
      fBarcodeFormat := '';
      if assigned(fOnScanCanceled) then
        fOnScanCanceled(self);
    end;
  end;
end;
{$ENDIF}

procedure TBarcodeScannerIntent.Scan;
{$IFDEF ANDROID}
var intent: jintent;
{$ENDIF}
begin
{$IFDEF ANDROID}
    // Callback registrieren
    FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);
    fBarcodeFormat := '';
    fbarcode:= '';
    Intent := TJIntent.JavaClass.init(StringToJString('com.google.zxing.client.android.SCAN'));
    Intent.setPackage(StringToJString('com.google.zxing.client.android'));
    if fCodetype <> 'then
      intent.putExtra(stringtojstring('SCAN_MODE'),stringtojstring(fCodetype));
    sharedactivity.startActivityForResult(intent,SCAN_REQUEST_CODE);
{$ENDIF}
end;


end.
Andreas
Monads? Wtf are Monads?

Geändert von QuickAndDirty (26. Jan 2017 um 17:33 Uhr)
  Mit Zitat antworten Zitat