码迷,mamicode.com
首页 > 移动开发 > 详细

Android实例-设置消息提醒(XE8+小米2)

时间:2015-09-04 19:45:46      阅读:276      评论:0      收藏:0      [点我收藏+]

标签:

技术分享

 

结果:

1.二个按钮可以新建消息提醒,最小化也是新建消息提醒。

2.程序必须最小化后才能点击消息提醒Label2才会有反映。

 

实例代码:

  1 unit Unit1;
  2 
  3 interface
  4 
  5 uses
  6   System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  7   FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  8   FMX.Notification, FMX.Controls.Presentation, FMX.StdCtrls,
  9   FMX.Platform;//需要引入
 10 
 11 type
 12   TForm1 = class(TForm)
 13     NotificationCenter1: TNotificationCenter;
 14     Button1: TButton;
 15     Button2: TButton;
 16     Label1: TLabel;
 17     Label2: TLabel;
 18     Button3: TButton;
 19     procedure Button1Click(Sender: TObject);
 20     procedure Button2Click(Sender: TObject);
 21     procedure Button3Click(Sender: TObject);
 22     procedure FormCreate(Sender: TObject);
 23     procedure NotificationCenter1ReceiveLocalNotification(Sender: TObject;
 24       ANotification: TNotification);
 25   private
 26     flag: Boolean;
 27     function HandleAppEvent(AAppEvent: TApplicationEvent; AContext: TObject): Boolean;
 28     { Private declarations }
 29   public
 30     { Public declarations }
 31   end;
 32 
 33 var
 34   Form1: TForm1;
 35 
 36 implementation
 37 {$R *.fmx}
 38 {$R *.NmXhdpiPh.fmx ANDROID}
 39 
 40 //延时新建一个消息提醒
 41 procedure TForm1.Button1Click(Sender: TObject);
 42 var
 43   MyNotification: TNotification;
 44 begin
 45   //通过消息中心创建消息
 46   MyNotification := NotificationCenter1.CreateNotification;
 47   try
 48     MyNotification.Name := 消息的名称; //设置消息的名称
 49     MyNotification.AlertBody := 消息的内容; //设置消息的内容
 50     MyNotification.Number := 18;//设置图标标号
 51     MyNotification.FireDate := Now + EncodeTime(0,0, 10, 0); //设置 10 秒后触发消息
 52     //将消息提交消息中心,并于指定时间触发
 53     NotificationCenter1.ScheduleNotification(MyNotification);
 54     Label2.Text := ‘‘;
 55   finally
 56     MyNotification.DisposeOf; //释放消息接口
 57   end;
 58 end;
 59 
 60 //即时新建消息提醒
 61 procedure TForm1.Button2Click(Sender: TObject);
 62 var
 63   MyNotification: TNotification;
 64 begin
 65   MyNotification :=NotificationCenter1.CreateNotification; //通过消息中心创建消息
 66   try
 67     MyNotification.Name:= 消息的名称; //设置消息的名称
 68     MyNotification.AlertBody := 消息的内容; //设置消息的内容
 69     MyNotification.Number := 18;//设置图标标号
 70     MyNotification.EnableSound := True;//有提示音
 71     NotificationCenter1.PresentNotification(MyNotification);  //将消息提交消息中心
 72     Label2.Text := ‘‘;
 73   finally
 74     MyNotification.DisposeOf; //释放消息接口
 75   end;
 76 end;
 77 
 78 //取消消息提醒
 79 procedure TForm1.Button3Click(Sender: TObject);
 80 begin
 81   NotificationCenter1.CancelNotification(消息的名称);
 82 end;
 83 
 84 //主要是为程序挂接事件
 85 procedure TForm1.FormCreate(Sender: TObject);
 86 var
 87   aFMXApplicationEventService: IFMXApplicationEventService;
 88 begin
 89   flag := True;
 90   if TPlatformServices.Current.SupportsPlatformService(IFMXApplicationEventService, IInterface(aFMXApplicationEventService)) then
 91     aFMXApplicationEventService.SetApplicationEventHandler(HandleAppEvent)
 92   else
 93   flag := False;
 94 end;
 95 
 96 //将要挂接在程序上的事件,此事件是最小化时新建一个消息提醒
 97 function TForm1.HandleAppEvent(AAppEvent: TApplicationEvent;
 98   AContext: TObject): Boolean;
 99 var
100   MyNotification: TNotification;
101 begin
102   if flag = False then
103     Exit;
104   case AAppEvent of
105     TApplicationEvent.aeEnteredBackground://监测,当程序后台运行时执行以下事件
106     begin
107       //通过消息中心创建消息
108       MyNotification := NotificationCenter1.CreateNotification;
109       try
110         MyNotification.Name :=消息的名称; //设置消息的名称
111         //设置消息的内容
112         MyNotification.AlertBody := 消息的内容;
113         MyNotification.Number := 18; //设置图标标号
114         MyNotification.EnableSound := True;
115         NotificationCenter1.PresentNotification(MyNotification); //将消息提交消息中心
116         Label2.Text := ‘‘;
117       finally
118         MyNotification.DisposeOf; //释放消息接口
119       end;
120     end;
121   end;
122   Result := True;
123 end;
124 
125 //程序最小化后,点消提醒时,发生此事件
126 procedure TForm1.NotificationCenter1ReceiveLocalNotification(Sender: TObject;
127   ANotification: TNotification);
128 begin
129   //收到消息后程序的操作
130   Label2.Text := 收到 + ANotification.Name + 的消息!;
131 end;
132 
133 end.

 

Android实例-设置消息提醒(XE8+小米2)

标签:

原文地址:http://www.cnblogs.com/FKdelphi/p/4782124.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!