delphi 应用程序未接收意向广播

mkshixfv  于 2022-11-04  发布在  其他
关注(0)|答案(1)|浏览(136)

Delphi Android移动的应用程序是用Delphi Alexandria(Delphi 11.1)开发的。
我从Zebra条形码扫描仪设备接收广播。
我尝试从Sunmi设备和Unitech设备接收广播,但无法正常工作。
Sunmi和Unitech均配置为使用广播输出条形码。
应用程序信息清单档案:

<!-- Zebra - OK -->
<intent-filter>
    <action android:name="fr.XXX.YYY.ACTION" />
    <category android:name="android.intent.category.DEFAULT" />             
</intent-filter>            
<intent-filter>
    <!-- SUNMI - Ko -->
    <action android:name="com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>
<intent-filter>
    <!-- Unitech - Ko -->
    <action android:name="unitech.scanservice.data" />
    <category android:name="android.intent.category.DEFAULT" />
</intent-filter>

在我的代码中,我注册了以下意图:

MainActivity.registerIntentAction(StringToJString(bcZebra));
MainActivity.registerIntentAction(StringToJString(bcSunmi));
MainActivity.registerIntentAction(StringToJString(bcUnitech));

然后我的过程广播这样说:

function TfData.HandleIntentAction(const Data: JIntent): Boolean;
var bc : string;
begin
  TOSLog.d('*********SCAN******************');
  TOSLog.d(JStringToString(Data.getAction));

  Result := False;
  if Data <> nil then begin
    // Zebra, bcZebraData = com.symbol.datawedge.data_string - Ok 
    if StringToJString(bcZebra).equals(Data.getAction) then begin
      bc := JStringToString(Data.getStringExtra(StringToJString(bcZebraData)));
      result := true;
      bcScan(bc);
    end

    // Sunmi, bcSunmiData = 'data' - Ko 
    else if StringToJString(bcSunmi).equals(Data.getAction) then begin
      bc := JStringToString(Data.getStringExtra(StringToJString(bcSunmiData)));
      result := true;
      bcScan(bc);
    end

    // Unitech, bcUnitechData = 'text' - Ko
    else if StringToJString(bcUnitech).equals(Data.getAction) then begin
      bc := JStringToString(Data.getStringExtra(StringToJString(bcUnitechData)));
      result := true;
      bcScan(bc);
    end;
  end;
end;

当我使用Zebra设备扫描条形码时,我可以在日志控制台中看到:


*********SCAN******************

fr.XXX.YYY.ACTION

我在Sunmi和Unitech中没有看到它,所以没有调用该函数。
但是,当我使用Sunmi设备进行扫描时,我在常规日志中得到以下信息:
正在从系统1269发送不受保护的广播com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED:com.sunmi.scanner/u 0a 74 pkg com.sunmi.scanner
与Unitech合作:
10月28日18时44分30秒。E/活动管理器(1178):从系统2360发送不受保护的广播unitech.scanservice.data:com.unitech.scanservice/1000 pkg com.unitech.scanservice
我不知道我错过了什么,由于Zebra在发送广播时不记录任何内容,我无法比较其他2台设备。

eni9jsuy

eni9jsuy1#

快速浏览一下Sunmi和Unitech的文档,您会发现需要使用注册的BroadcastReceiver并执行相应的操作来完成此操作。
第一个单元定义了一个类,它将处理任何操作:

unit MultiReceiver;

interface

uses
  Androidapi.JNIBridge, Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.Embarcadero;

type
  TMultiReceiver = class;

  TMultiReceiverListener = class(TJavaLocal, JFMXBroadcastReceiverListener)
  private
    FMultiReceiver: TMultiReceiver;
  public
    { JFMXBroadcastReceiverListener }
    procedure onReceive(context: JContext; intent: JIntent); cdecl;
  public
    constructor Create(const AMultiReceiver: TMultiReceiver);
  end;

  TIntentEvent = procedure(Sender: TObject; const Intent: JIntent) of object;

  TMultiReceiver = class(TObject)
  private
    FBroadcastReceiver: JFMXBroadcastReceiver;
    FIntentFilter: JIntentFilter;
    FReceiverListener: TMultiReceiverListener;
    FOnIntent: TIntentEvent;
  protected
    procedure Receive(context: JContext; intent: JIntent); virtual;
    procedure ConfigureActions; virtual; abstract;
    property IntentFilter: JIntentFilter read FIntentFilter;
  public
    constructor Create;
    destructor Destroy; override;
    property OnIntent: TIntentEvent read FOnIntent write FOnIntent;
  end;

implementation

uses
  Androidapi.Helpers;

{ TMultiReceiverListener }

constructor TMultiReceiverListener.Create(const AMultiReceiver: TMultiReceiver);
begin
  inherited Create;
  FMultiReceiver := AMultiReceiver;
end;

procedure TMultiReceiverListener.onReceive(context: JContext; intent: JIntent);
begin
  FMultiReceiver.Receive(context, intent);
end;

{ TMultiReceiver }

constructor TMultiReceiver.Create;
begin
  inherited Create;
  FReceiverListener := TMultiReceiverListener.Create(Self);
  FBroadcastReceiver := TJFMXBroadcastReceiver.JavaClass.init(FReceiverListener);
  FIntentFilter := TJIntentFilter.JavaClass.init;
  ConfigureActions;
  TAndroidHelper.Context.registerReceiver(FBroadcastReceiver, FIntentFilter)
end;

destructor TMultiReceiver.Destroy;
begin
  TAndroidHelper.Context.unregisterReceiver(FBroadcastReceiver)
end;

procedure TMultiReceiver.Receive(context: JContext; intent: JIntent);
begin
  if Assigned(FOnIntent) then
    FOnIntent(Self, intent);
end;

end.

下面是一个如何使用它的示例:

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes,
  MultiReceiver;

type
  TScannerReceiver = class(TMultiReceiver)
  protected
    procedure ConfigureActions; override;
  end;

  TForm1 = class(TForm)
  private
    FScannerReceiver: TScannerReceiver;
    procedure ScannerReceiverIntentHandler(Sender: TObject; const AIntent: JIntent);
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

uses
  Androidapi.Helpers;

const
  cActionSunmi = 'com.sunmi.scanner.ACTION_DATA_CODE_RECEIVED';
  cActionUnitech = 'unitech.scanservice.data';

{ TScannerReceiver }

procedure TScannerReceiver.ConfigureActions;
begin
  IntentFilter.addAction(StringToJString(cActionSunmi));
  IntentFilter.addAction(StringToJString(cActionUnitech));
end;

{ TForm1 }

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  FScannerReceiver := TScannerReceiver.Create;
  FScannerReceiver.OnIntent := ScannerReceiverIntentHandler;
end;

procedure TForm1.ScannerReceiverIntentHandler(Sender: TObject; const AIntent: JIntent);
begin
  if AIntent.getAction.Equals(StringToJString(cActionSunmi)) then
  begin
    // Handle Sunmi
  end
  else if AIntent.getAction.Equals(StringToJString(cActionUnitech)) then
  begin
    // Handle Unitech
  end;
end;

end.

注意:这是完全未经测试的,因为我没有任何一个模型由它处理

相关问题