Android实例-退出程序
Android实例-退出程序
http://www.cnblogs.com/FKdelphi
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.Controls.Presentation, FMX.StdCtrls; type TForm1 = class(TForm) Button1: TButton; Button2: TButton; Button3: TButton; Label1: TLabel; Button4: TButton; Button5: TButton; Button6: TButton; procedure Button1Click(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button3Click(Sender: TObject); procedure FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); procedure Button4Click(Sender: TObject); procedure Button6Click(Sender: TObject); procedure Button5Click(Sender: TObject); private { Private declarations } public { Public declarations } end; var Form1: TForm1; implementation uses FMX.Platform.Android, FMX.Helpers.Android, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewText, Androidapi.JNI.JavaTypes, Androidapi.Helpers, system.IOUtils; {$R *.fmx} {$R *.NmXhdpiPh.fmx ANDROID} {$R *.LgXhdpiPh.fmx ANDROID} {$R *.Macintosh.fmx MACOS} {$R *.XLgXhdpiTb.fmx ANDROID} {$R *.LgXhdpiTb.fmx ANDROID} {$R *.GGlass.fmx ANDROID} procedure TForm1.Button1Click(Sender: TObject); begin Label1.Text := '按返回键将会出现一个对话框!'; end; procedure TForm1.Button2Click(Sender: TObject); begin MainActivity.finish; { 退出程序 } end; procedure TForm1.Button3Click(Sender: TObject); begin {$IFDEF ANDROID} Application.MainForm.DisposeOf; { 退出程序 } halt(0); {$ENDIF} end; procedure TForm1.Button4Click(Sender: TObject); var Intent: JIntent; begin Intent := TJIntent.JavaClass.init(TJIntent.JavaClass.ACTION_MAIN); Intent.addCategory(TJIntent.JavaClass.CATEGORY_HOME); Intent.setFlags(TJIntent.JavaClass.FLAG_ACTIVITY_NEW_TASK); SharedActivityContext.startActivity(Intent); end; procedure TForm1.Button5Click(Sender: TObject); begin Application.Terminate; end; procedure TForm1.Button6Click(Sender: TObject); begin Close; end; //此代码报错,不可用,原因不明。 procedure TForm1.FormKeyUp(Sender: TObject; var Key: Word; var KeyChar: Char; Shift: TShiftState); begin if Key = vkHardwareBack then//如果按下物理返回键 begin Label1.Text := '1'; if MessageDlg('确认退出吗?', TMsgDlgType.mtConfirmation, [TMsgDlgBtn.mbOK, TMsgDlgBtn.mbCancel], -1) = mrOK then MainActivity.finish; { 退出程序 } Key := 0;//必须的,不然按否也会退出 Exit; end; end; end.