1. 程式人生 > >Android例項-設定訊息提醒(XE8+小米2)

Android例項-設定訊息提醒(XE8+小米2)

https://www.cnblogs.com/FKdelphi/p/4782124.html

相關資料:

1.官網例項:http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Notification_Center_(iOS_and_Android)

 

結果:

1.二個按鈕可以新建訊息提醒,最小化也是新建訊息提醒。

2.程式必須最小化後才能點選訊息提醒Label2才會有反映。

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Notification, FMX.Controls.Presentation, FMX.StdCtrls,
  FMX.Platform;//需要引入

type
  TForm1 = class(TForm)
    NotificationCenter1: TNotificationCenter;
    Button1: TButton;
    Button2: TButton;
    Label1: TLabel;
    Label2: TLabel;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure NotificationCenter1ReceiveLocalNotification(Sender: TObject;
      ANotification: TNotification);
  private
    flag: Boolean;
    function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID}

//延時新建一個訊息提醒
procedure TForm1.Button1Click(Sender: TObject);
var
  MyNotification: TNotification;
begin
  //通過訊息中心建立訊息
  MyNotification := NotificationCenter1.CreateNotification;
  try
    MyNotification.Name := '訊息的名稱'; //設定訊息的名稱
    MyNotification.AlertBody := '訊息的內容'; //設定訊息的內容
    MyNotification.Number := 18;//設定圖示標號
    MyNotification.FireDate := Now + EncodeTime(0,0, 10, 0); //設定 10 秒後觸發訊息
    //將訊息提交訊息中心,並於指定時間觸發
    NotificationCenter1.ScheduleNotification(MyNotification);
    Label2.Text := '';
  finally
    MyNotification.DisposeOf; //釋放訊息介面
  end;
end;

//即時新建訊息提醒
procedure TForm1.Button2Click(Sender: TObject);
var
  MyNotification: TNotification;
begin
  MyNotification :=NotificationCenter1.CreateNotification; //通過訊息中心建立訊息
  try
    MyNotification.Name:= '訊息的名稱'; //設定訊息的名稱
    MyNotification.AlertBody := '訊息的內容'; //設定訊息的內容
    MyNotification.Number := 18;//設定圖示標號
    MyNotification.EnableSound := True;//有提示音
    NotificationCenter1.PresentNotification(MyNotification);  //將訊息提交訊息中心
    Label2.Text := '';
  finally
    MyNotification.DisposeOf; //釋放訊息介面
  end;
end;

//取消訊息提醒
procedure TForm1.Button3Click(Sender: TObject);
begin
  NotificationCenter1.CancelNotification('訊息的名稱');
end;

//主要是為程式掛接事件
procedure TForm1.FormCreate(Sender: TObject);
var
  aFMXApplicationEventService: IFMXApplicationEventService;
begin
  flag := True;
  if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
    aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
  else
  flag := False;
end;

//將要掛接在程式上的事件,此事件是最小化時新建一個訊息提醒
function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent;
  AContext: TObject): Boolean;
var
  MyNotification: TNotification;
begin
  if flag = False then
    Exit;
  case AAppEvent of
    TApplicationEvent.aeEnteredBackground://監測,當程式後臺執行時執行以下事件
    begin
      //通過訊息中心建立訊息
      MyNotification := NotificationCenter1.CreateNotification;
      try
        MyNotification.Name :='訊息的名稱'; //設定訊息的名稱
        //設定訊息的內容
        MyNotification.AlertBody := '訊息的內容';
        MyNotification.Number := 18; //設定圖示標號
        MyNotification.EnableSound := True;
        NotificationCenter1.PresentNotification(MyNotification); //將訊息提交訊息中心
        Label2.Text := '';
      finally
        MyNotification.DisposeOf; //釋放訊息介面
      end;
    end;
  end;
  Result := True;
end;

//程式最小化後,點消提醒時,發生此事件
procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
  ANotification: TNotification);
begin
  //收到訊息後程序的操作
  Label2.Text := '收到' + ANotification.Name + '的訊息!';
end;

end.