捕获几个事件,各种手机的全局按键
分类:多线程

以下代码能监控到以下几个事件:
FinishedLaunching
BecameActive
WillBecomeInactive
EnteredBackground
WillBecomeForeground
WillTerminate
LowMemory
TimeChange
OpenURL

直接上代码,还有条经验就是SetApplicationEventHandler可注册多个事件方法。

unit Dxc;
interface
uses  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.StdCtrls,  FMX.Layouts, FMX.Memo,FMX.Platform.Android, FMX.Edit,System.Math, FMX.ListBox;
type  TSendSMSForm = class(TForm)    StatusBar1: TStatusBar;    ToolBar1: TToolBar;    Memo1: TMemo;    Button1: TButton;    StyleBook1: TStyleBook;    Button2: TButton;    Button3: TButton;    Memo2: TMemo;    Label1: TLabel;    Edit1: TEdit;    Edit2: TEdit;    Label2: TLabel;    Label3: TLabel;    Label4: TLabel;    Label5: TLabel;    Label6: TLabel;    Button4: TButton;    Panel1: TPanel;    Panel2: TPanel;    Panel3: TPanel;    Panel4: TPanel;    Panel5: TPanel;    Panel6: TPanel;    Panel7: TPanel;    procedure Button1Click(Sender: TObject);    procedure Button3Click(Sender: TObject);    procedure Button2Click(Sender: TObject);    procedure Button4Click(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;
var  SendSMSForm: TSendSMSForm;  myThread: TThread;implementation
{$R *.fmx}
uses  Androidapi.JNI.JavaTypes,Androidapi.JNI.Telephony,System.IoUtils,androidapi.IOUtils;

unit Unit11;

unit Unit6;

function SendSMS(tel,str:string):Boolean;var  j: JSmsManager;begin  j := tjsmsmanager.Create;  j.sendTextMessage(stringtojstring(tel), nil, stringtojstring(str), nil, nil);  Result := True;end;

interface

interface

procedure MyMethod;var  i,x: Integer;  str:string;  tel:string;  s:string;begin  for i := 0 to SendSMSForm.Memo2.Lines.Count -1 do  begin   // str:=SendSMSForm.Memo1.Lines.Text+datetimetostr(Now);    str:=SendSMSForm.Memo1.Lines.Text;    tel:=SendSMSForm.Memo2.Lines.Strings[i];    if (Trim(tel)<>'') and (Trim(str)<>'') then begin     SendSMS(tel,str);    end;    x:= RandomRange(StrToInt(SendSMSForm.Edit1.Text)*1000,StrToInt(SendSMSForm.Edit2.Text)*1000);    Sleep(x);  end;end;
procedure TSendSMSForm.Button1Click(Sender: TObject);begin myThread := TThread.CreateAnonymousThread(MyMethod); //!!! myThread.Start;end;
procedure TSendSMSForm.Button2Click(Sender: TObject);begin  MainActivity.finish;end;
procedure TSendSMSForm.Button3Click(Sender: TObject);begin myThread.suspend;end;
procedure TSendSMSForm.Button4Click(Sender: TObject);var i:Integer; s,m:string;begin  for s in TDirectory.GetFiles(Androidapi.IOUtils.GetSharedCameraDir) do   Memo1.Lines.Add(s);   for s in TDirectory.GetDirectories('/storage')do   Memo1.Lines.Add(s);   for s in TDirectory.GetDirectoryRoot('/storage/sdcard0/DCIM/Camera')do   Memo1.Lines.Add(s);   Memo1.Lines.Add('/////////////////////////////////////////////////');    for s in TDirectory.GetDirectories(TDirectory.GetDirectoryRoot('/storage/sdcard0/DCIM/Camera'))do   Memo1.Lines.Add(s);end;
end.

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts,
  FMX.Memo, FMX.Platform, FMX.StdCtrls;

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.ScrollBox, FMX.Memo, FMX.Platform;

type
  TForm11 = class(TForm)
    Memo1: TMemo;
    ToolBar1: TToolBar;
    Label1: TLabel;
    procedure FormCreate(Sender: TObject);
  private
    procedure Log(s: string);
  public
    function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
  end;

type
  TForm6 = class(TForm)
    Memo1: TMemo;
    procedure FormCreate(Sender: TObject);
  private
    function HandleAppEvent(AAppEvent: TApplicationEvent;
      AContext: TObject): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form11: TForm11;

var
  Form6: TForm6;

implementation

implementation

{$R *.fmx}

{$R *.fmx}

{ TForm11 }

procedure TForm6.FormCreate(Sender: TObject);
var
  SvcEvents: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService
    (IFMXApplicationEventService, IInterface(SvcEvents))
  then
    SvcEvents.SetApplicationEventHandler(HandleAppEvent);

procedure TForm11.FormCreate(Sender: TObject);
var aFMXApplicationEventService: IFMXApplicationEventService;
begin
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
    aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
  else
    Log('Application Event Service is not supported.');
end;

end;

function TForm11.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
begin
  case AAppEvent of
    aeFinishedLaunching: Log('Finished Launching');
    aeBecameActive: Log('Became Active');
    aeWillBecomeInactive: Log('Will Become Inactive');
    aeEnteredBackground: Log('Entered Background');
    aeWillBecomeForeground: Log('Will Become Foreground');
    aeWillTerminate: Log('Will Terminate');
    aeLowMemory: Log('Low Memory');
    aeTimeChange: Log('Time Change');
    aeOpenURL: Log('Open URL');
  end;
  Result := True;
end;

function TForm6.HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
var
  astate: string;
begin
  case AAppEvent of
    TApplicationEvent.FinishedLaunching:
      astate := 'FinishedLaunching';
    TApplicationEvent.BecameActive:
      astate := 'BecameActive';
    TApplicationEvent.WillBecomeInactive:
      astate := 'WillBecomeInactive';
    TApplicationEvent.EnteredBackground:
      astate := 'EnteredBackground';
    TApplicationEvent.WillBecomeForeground:
      astate := 'WillBecomeForeground';
    TApplicationEvent.WillTerminate:
      astate := 'WillTerminate';
    TApplicationEvent.LowMemory:
      astate := 'LowMemory';
    TApplicationEvent.TimeChange:
      astate := 'TimeChange';
    TApplicationEvent.OpenURL:
      astate := 'OpenURL';
  end;
  Self.Memo1.Lines.Add(astate);
  Result := true;
end;

procedure TForm11.Log(s: string);
begin
  Memo1.Lines.Add(TimeToStr(Now) + ': ' + s);
end;

end.

end.

 

本文由10bet手机官网发布于多线程,转载请注明出处:捕获几个事件,各种手机的全局按键

上一篇:没有了 下一篇:没有了
猜你喜欢
热门排行
精彩图文