我正在使用Delphi 10.3和一个可以从手机读取SMS的组件。 这些组件的工作方式如下:
cmp.ReadSms;
此后,将发生一个事件onReadSMS
,该事件将针对出现的每条消息进行调用。
我需要将每个消息保存在数据库中,但是数据库对象位于我的主过程中。我无法通过活动访问我的数据库。
我想知道是否有一种方法可以将数据库对象传递给事件,以便可以在那里进行插入。
编辑:
这是一些代码:
unit myUnit;
TEventHandlers = class
procedure ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
end;
var myEvents: TEventHandlers;
function SMSReceive(dbMain: TFDConnection): Boolean;
var cSMS: TGSMObj;
begin
cSMS: TGSMObj.create(nil);
cSMS.OnReadMessage := myEvents.ReadMessage;
cSMS.ReadSms;
end;
procedure TEventHandlers.ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
// Here I can read the SMS.
// I would like to do:
// dbMain.ExecSQL('INSERT INTO ...')
// But dbMain is unknown here.
end;
我还要补充一点,该单元是从某个外部线程调用的,因此我不能拥有全局数据库对象,但是在调用SMSReceive
函数时需要传递它。
答案 0 :(得分:2)
您无法将自定义参数添加到预先存在的事件中(无需更改组件的源代码)。但是您可以尝试一些选项来操纵事件处理程序的操作方式。
最简单的解决方案是从TGSMObj
派生一个新类,并向其中添加一个TFDConnection
字段,例如:
type
TMyGSMObj = class(TGSMObj)
DB: TFDConnection;
end;
// 'class' methods can be used as event handlers, so you don't
// need to create a global object instance of this class...
TEventHandlers = class
class procedure ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
end;
function SMSReceive(dbMain: TFDConnection): Boolean;
var
cSMS: TMyGSMObj;
begin
cSMS := TMyGSMObj.Create(nil);
cSMS.DB := dbMain;
cSMS.OnReadMessage := TEventHandlers.ReadMessage;
cSMS.ReadSms;
end;
class procedure TEventHandlers.ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
TMyGSMObj(Sender).DB.ExecSQL('INSERT INTO ...');
end;
或者,如果TGSMObj
是TComponent
的后代,则可以使用其Tag
属性来保存TFDConnection
指针,例如:
type
TEventHandlers = class
class procedure ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
end;
function SMSReceive(dbMain: TFDConnection): Boolean;
var
cSMS: TGSMObj;
begin
cSMS := TGSMObj.Create(nil);
cSMS.Tag := NativeInt(dbMain);
cSMS.OnReadMessage := TEventHandlers.ReadMessage;
cSMS.ReadSms;
end;
class procedure TEventHandlers.ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
TFDConnection(TGSMObj(Sender).Tag).ExecSQL('INSERT INTO ...');
end;
或者,您可以使用TMethod
记录来强制事件处理程序的Self
参数指向您想要的任何内容,例如:
type
TEventHandlers = class
class procedure ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
end;
function SMSReceive(dbMain: TFDConnection): Boolean;
var
cSMS: TGSMObj;
Handler: TWhateverTypeOnReadMessageUses;
begin
Handler := TEventHandlers.ReadMessage;
TMethod(Handler).Data := dbMain;
cSMS := TGSMObj.Create(nil);
cSMS.OnReadMessage := Handler;
cSMS.ReadSms;
end;
class procedure TEventHandlers.ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
TFDConnection(Self).ExecSQL('INSERT INTO ...');
end;
或者,无论如何使用TMethod
,您可以使用独立过程而不是类方法,例如:
procedure ReadMessage(Self: Pointer; Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
TFDConnection(Self).ExecSQL('INSERT INTO ...');
end;
function SMSReceive(dbMain: TFDConnection): Boolean;
var
cSMS: TGSMObj;
Handler: TWhateverTypeOnReadMessageUses;
begin
TMethod(Handler).Data := dbMain;
TMethod(Handler).Code := @ReadMessage;
cSMS := TGSMObj.Create(nil);
cSMS.OnReadMessage := Handler;
cSMS.ReadSms;
end;
或者,class helper可能有用(我自己还没有尝试过),例如:
type
TFDConnectionHelper = class helper for TFDConnection
public
procedure ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
end;
function SMSReceive(dbMain: TFDConnection): Boolean;
var
cSMS: TGSMObj;
begin
cSMS := TGSMObj.Create(nil);
cSMS.OnReadMessage := dbMain.ReadMessage;
cSMS.ReadSms;
end;
procedure TFDConnectionHelper.ReadMessage(Sender: TObject; MessageText: WideString; PhoneNumber, CenterNumber: AnsiString; TimeStamp: TDateTime; TimeZone, Status, Index: Integer);
begin
Self.ExecSQL('INSERT INTO ...');
end;