Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)
源文地址:
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 type 13 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 private 26 //电话服务 27 PhoneDialerService: IFMXPhoneDialerService; 28 { Private declarations } 29 public 30 //需要覆盖 31 constructor Create(AOwner: TComponent); override; 32 { Public declarations } 33 end; 34 35 var 36 Form1: TForm1; 37 38 implementation 39 40 {$R *.fmx} 41 {$R *.NmXhdpiPh.fmx ANDROID} 42 43 { TForm1 } 44 45 //获取电话服务 46 constructor TForm1.Create(AOwner: TComponent); 47 begin 48 inherited Create(AOwner); 49 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService)); 50 end; 51 52 //获取运营商 53 procedure TForm1.Button1Click(Sender: TObject); 54 begin 55 { test whether the PhoneDialer services are supported on your device } 56 if Assigned(PhoneDialerService) then 57 begin 58 { 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 type 13 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 private 26 //电话服务 27 PhoneDialerService: IFMXPhoneDialerService; 28 { Private declarations } 29 public 30 //需要覆盖 31 constructor Create(AOwner: TComponent); override; 32 { Public declarations } 33 end; 34 35 var 36 Form1: TForm1; 37 38 implementation 39 40 {$R *.fmx} 41 {$R *.NmXhdpiPh.fmx ANDROID} 42 43 { TForm1 } 44 //获取电话服务 45 constructor TForm1.Create(AOwner: TComponent); 46 begin 47 inherited Create(AOwner); 48 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService)); 49 end; 50 51 //打电话功能 52 procedure TForm1.Button2Click(Sender: TObject); 53 begin 54 { test whether the PhoneDialer services are supported on your device } 55 if Assigned(PhoneDialerService) then 56 begin 57 { if the Telephone Number is entered in the edit box then make the call, else 58 display an error message } 59 if edtTelephoneNumber.Text <> '' then 60 PhoneDialerService.Call(edtTelephoneNumber.Text) 61 else 62 begin 63 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 type 13 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 private 26 //电话服务 27 PhoneDialerService: IFMXPhoneDialerService; 28 //定义方法 29 procedure MyOnCallStateChanged(const ACallID: String; const ACallState: TCallState); 30 { Private declarations } 31 public 32 //需要覆盖 33 constructor Create(AOwner: TComponent); override; 34 { Public declarations } 35 end; 36 37 var 38 Form1: TForm1; 39 40 implementation 41 42 {$R *.fmx} 43 {$R *.NmXhdpiPh.fmx ANDROID} 44 45 { TForm1 } 46 47 //获取电话服务 48 constructor TForm1.Create(AOwner: TComponent); 49 begin 50 inherited Create(AOwner); 51 TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService)); 52 if Assigned(PhoneDialerService) then 53 PhoneDialerService.OnCallStateChanged := MyOnCallStateChanged; 54 end; 55 56 //检测呼叫状态的变化 57 procedure TForm1.MyOnCallStateChanged(const ACallID: String; 58 const ACallState: TCallState); 59 var outText: String; 60 begin 61 case ACallState of 62 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 | 调用断开连接。 | + | + |
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我