Delphi在Android下实现BroadcastReceiver功能(举例在Delphi下获取USB外设拔插消息)

在Android里,用java通过实现BroadcastReceiver接口,就可以获得Intent消息。可是Delphi程序不能直接实现JBroadcastReceiver,如何能够实现类似Java的BroadcastReceiver功能呢?

幸运的是Delphi提供了TJFMXBroadcastReceiver类实现了BroadcastReceiver,我们便可以以此达到目的。

下面的程序片段举例,实现如何在Delphi下获取USB外设拔插的消息。

 

 

uses System.SysUtils, System.Classes,
  System.Generics.Collections,
  Androidapi.JNI.USB,
  Androidapi.JNI.Embarcadero,
  Androidapi.JNIBridge,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.App,
  Androidapi.JNI.os,...;

  TUsbFtdiBroadcastReceiverListener = class(TJavaLocal, JFMXBroadcastReceiverListener)
  private
    [Weak] FPort: TFTDIPort;
  public
    constructor Create(const Owner: TFTDIPort);
    procedure onReceive(context: JContext; intent: JIntent); cdecl;
  end;

  TFTDIPort = class(TInterfacedObject, ICommPort)
  protected
    class var FManager: JD2xxManager;
    class var FListener : TUsbFtdiBroadcastReceiverListener;
    class var FReceiver: JFMXBroadcastReceiver;
    class var FUsbManager:JUsbManager;
    class var FFilter:JIntentFilter;
    class procedure InitClass(Port:TFtdiPort);
  public
    function Connect(Device : String): Boolean;
    procedure Disconnect;
    ...... 
  end;



{ TUsbFtdiBroadcastReceiverListener }
const
  ACTION_USB_PERMISSION='com.android.example.USB_PERMISSION';

constructor TUsbFtdiBroadcastReceiverListener.Create(const Owner: TFtdiPort);
begin
  inherited Create;
  FPort:=Owner;
end;

procedure TUsbFtdiBroadcastReceiverListener.onReceive(context: JContext;
  intent: JIntent);
var
  action:String;
  Accessory:JUsbAccessory;
begin
  action:=JStringToString(intent.getAction);
  if action=ACTION_USB_PERMISSION then
  begin
    if (intent.getBooleanExtra(TJUsbManager.JavaClass.EXTRA_PERMISSION_GRANTED, False)) then
    begin
      Accessory:= TJUsbAccessory.Wrap(intent.getParcelableExtra(TJUsbManager.JavaClass.EXTRA_ACCESSORY));
      if JStringToString(Accessory.getManufacturer)='FTDI' then
      begin
        FPort.Connect(JStringToString(Accessory.toString));
      end;
    end;
  end
  else if (action=TJUsbManager_ACTION_USB_DEVICE_DETACHED) then
  begin
    FPort.Disconnect;
  end
  else
  begin
  end;
end;

class procedure TFtdiPort.InitClass(Port:TFdiPort);
beginif not Assigned(FUsbManager) then
  begin
    FUsbManager := TJUsbManager.Wrap(SharedActivityContext.getSystemService(TJContext.JavaClass.USB_SERVICE));
    if not Assigned(FUsbManager) then Exit;
    FListener := TUsbFtdiBroadcastReceiverListener.Create(Port);
    if not Assigned(FListener) then Exit;
    FReceiver := TJFMXBroadcastReceiver.JavaClass.init(FListener);
    if not Assigned(FReceiver) then Exit;

    FFilter:=TJIntentFilter.JavaClass.init(StringToJString(ACTION_USB_PERMISSION));
    if not Assigned(FFilter) then Exit;
    FFilter.addAction(TJUsbManager.JavaClass.ACTION_USB_ACCESSORY_DETACHED);

    SharedActivityContext.registerReceiver(FReceiver, FFilter);
  end;
end;

 

posted @ 2014-09-18 02:55  子航  阅读(3477)  评论(1编辑  收藏  举报