源文地址:
http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Phone_Dialer_on_Mobile_Devices_(iOS_and_Android)
1.获取运营商、国家代码
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, FMX.Controls.Presentation, 8 FMX.StdCtrls, FMX.Edit, 9 FMX.Platform,//需要引入10 FMX.PhoneDialer;//需要引入11 12 type13 TForm1 = class(TForm)14 Button1: TButton;15 lblCarrierName: TEdit;16 lblISOCountryCode: TEdit;17 Button2: TButton;18 edtTelephoneNumber: TEdit;19 lblCallState: TEdit;20 Label1: TLabel;21 Label2: TLabel;22 Label3: TLabel;23 procedure Button1Click(Sender: TObject);24 procedure Button2Click(Sender: TObject);25 private26 //电话服务27 PhoneDialerService: IFMXPhoneDialerService;28 { Private declarations }29 public30 //需要覆盖31 constructor Create(AOwner: TComponent); override;32 { Public declarations }33 end;34 35 var36 Form1: TForm1;37 38 implementation39 40 { $R *.fmx}41 { $R *.NmXhdpiPh.fmx ANDROID}42 43 { TForm1 }44 45 //获取电话服务46 constructor TForm1.Create(AOwner: TComponent);47 begin48 inherited Create(AOwner);49 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));50 end;51 52 //获取运营商53 procedure TForm1.Button1Click(Sender: TObject);54 begin55 { test whether the PhoneDialer services are supported on your device }56 if Assigned(PhoneDialerService) then57 begin58 { if yes, then update the labels with the retrieved information }59 lblCarrierName.Text := 'Carrier Name: ' + PhoneDialerService.GetCarrier.GetCarrierName;60 lblISOCountryCode.Text := 'ISO Country Code: ' + PhoneDialerService.GetCarrier.GetIsoCountryCode;61 end;62 end;63 64 end.
2.实现打电话功能
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, FMX.Controls.Presentation, 8 FMX.StdCtrls, FMX.Edit, 9 FMX.Platform,//需要引入10 FMX.PhoneDialer;//需要引入11 12 type13 TForm1 = class(TForm)14 Button1: TButton;15 lblCarrierName: TEdit;16 lblISOCountryCode: TEdit;17 Button2: TButton;18 edtTelephoneNumber: TEdit;19 lblCallState: TEdit;20 Label1: TLabel;21 Label2: TLabel;22 Label3: TLabel;23 procedure Button1Click(Sender: TObject);24 procedure Button2Click(Sender: TObject);25 private26 //电话服务27 PhoneDialerService: IFMXPhoneDialerService;28 { Private declarations }29 public30 //需要覆盖31 constructor Create(AOwner: TComponent); override;32 { Public declarations }33 end;34 35 var36 Form1: TForm1;37 38 implementation39 40 { $R *.fmx}41 { $R *.NmXhdpiPh.fmx ANDROID}42 43 { TForm1 }44 //获取电话服务45 constructor TForm1.Create(AOwner: TComponent);46 begin47 inherited Create(AOwner);48 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));49 end;50 51 //打电话功能52 procedure TForm1.Button2Click(Sender: TObject);53 begin54 { test whether the PhoneDialer services are supported on your device }55 if Assigned(PhoneDialerService) then56 begin57 { if the Telephone Number is entered in the edit box then make the call, else58 display an error message }59 if edtTelephoneNumber.Text <> '' then60 PhoneDialerService.Call(edtTelephoneNumber.Text)61 else62 begin63 ShowMessage('Please type-in a telephone number.');64 edtTelephoneNumber.SetFocus;65 end;66 end;67 end;68 69 end.
3.检测呼叫状态的变化
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, FMX.Controls.Presentation, 8 FMX.StdCtrls, FMX.Edit, 9 FMX.Platform,//需要引入10 FMX.PhoneDialer;//需要引入11 12 type13 TForm1 = class(TForm)14 Button1: TButton;15 lblCarrierName: TEdit;16 lblISOCountryCode: TEdit;17 Button2: TButton;18 edtTelephoneNumber: TEdit;19 lblCallState: TEdit;20 Label1: TLabel;21 Label2: TLabel;22 Label3: TLabel;23 procedure Button1Click(Sender: TObject);24 procedure Button2Click(Sender: TObject);25 private26 //电话服务27 PhoneDialerService: IFMXPhoneDialerService;28 //定义方法29 procedure MyOnCallStateChanged(const ACallID: String; const ACallState: TCallState);30 { Private declarations }31 public32 //需要覆盖33 constructor Create(AOwner: TComponent); override;34 { Public declarations }35 end;36 37 var38 Form1: TForm1;39 40 implementation41 42 { $R *.fmx}43 { $R *.NmXhdpiPh.fmx ANDROID}44 45 { TForm1 }46 47 //获取电话服务48 constructor TForm1.Create(AOwner: TComponent);49 begin50 inherited Create(AOwner);51 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));52 if Assigned(PhoneDialerService) then53 PhoneDialerService.OnCallStateChanged := MyOnCallStateChanged;54 end;55 56 //检测呼叫状态的变化57 procedure TForm1.MyOnCallStateChanged(const ACallID: String;58 const ACallState: TCallState);59 var outText: String;60 begin61 case ACallState of62 FMX.PhoneDialer.TCallState.None: outText := 'No calls';63 FMX.PhoneDialer.TCallState.Connected: outText := 'Connected';64 FMX.PhoneDialer.TCallState.Incoming: outText := 'Incoming call';65 FMX.PhoneDialer.TCallState.Dialing: outText := 'Dialing';66 FMX.PhoneDialer.TCallState.Disconnected: outText := 'Disconnected';67 end;68 lblCallState.Text := outText;69 end;70 71 end.
PS:
的 IFMXPhoneDialerService 接口提供了 OnCallStateChanged 事件,允许您处理呼叫状态的变化。 的 TCallState 枚举描述可能的电话状态。
下面的表描述的物品 TCallState 枚举(为每个平台支持的州是标有“+”)。
项 | 描述 | iOS | 安卓 |
---|---|---|---|
csNone | 没有呼叫状态。 | + | - - - - - - |
csConnected | 电话调用者与被调用方。 | + | + |
csIncoming | 传入的电话。 | + | + |
csDialing | 手机在拨号状态。 | + | - - - - - - |
csDisconnected | 调用断开连接。 | + | + |