Android实例-设置消息提醒(XE8+小米2)
2015-09-04 18:15
417 查看
相关资料:
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.
相关文章推荐
- android 怎么选择audio hal
- android开发常用网址
- android support Percent支持库开发
- android:contentDescription 的用途
- Android笔记(二十一) Android中的Adapter
- [Android Pro] android 混淆文件project.properties和proguard-project.txt
- 【Android】简单详细的安卓开发环境配置及运行第一个程序Helloworld
- android开发过程中的tips
- 兼容Android API 2.X的涟漪点击效果
- 解决Android AVD启动报错问题
- android事件分发机制
- Android 通过资源名,获取资源ID
- Android学习笔记之Json的使用....
- Android实例-为程序创建快捷方式(未测试)
- 关于 Android Studio 鼠标中键的一些技巧
- Android自定义动画--卡牌翻牌动画
- Android简单ListView练习记录
- 关于android的屏幕保持常亮
- Android基础入门教程——6.3.1 数据存储与访问之——初见SQLite数据库
- 最新基于adt-bundle-windows-x86的android开发环境搭建