Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)

时间:2021-08-15 10:54:41

Android实例-使用电话拨号器在移动设备上(官方)(XE8+小米2)

源文地址:

http://docwiki.embarcadero.com/RADStudio/XE5/en/Mobile_Tutorial:_Using_the_Phone_Dialer_on_Mobile_Devices_(iOS_and_Android)

1.获取运营商、国家代码

 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, FMX.Edit,
FMX.Platform,//需要引入
FMX.PhoneDialer;//需要引入 type
TForm1 = class(TForm)
Button1: TButton;
lblCarrierName: TEdit;
lblISOCountryCode: TEdit;
Button2: TButton;
edtTelephoneNumber: TEdit;
lblCallState: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
//电话服务
PhoneDialerService: IFMXPhoneDialerService;
{ Private declarations }
public
//需要覆盖
constructor Create(AOwner: TComponent); override;
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} { TForm1 } //获取电话服务
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));
end; //获取运营商
procedure TForm1.Button1Click(Sender: TObject);
begin
{ test whether the PhoneDialer services are supported on your device }
if Assigned(PhoneDialerService) then
begin
{ if yes, then update the labels with the retrieved information }
lblCarrierName.Text := 'Carrier Name: ' + PhoneDialerService.GetCarrier.GetCarrierName;
lblISOCountryCode.Text := 'ISO Country Code: ' + PhoneDialerService.GetCarrier.GetIsoCountryCode;
end;
end; end.

2.实现打电话功能

 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, FMX.Edit,
FMX.Platform,//需要引入
FMX.PhoneDialer;//需要引入 type
TForm1 = class(TForm)
Button1: TButton;
lblCarrierName: TEdit;
lblISOCountryCode: TEdit;
Button2: TButton;
edtTelephoneNumber: TEdit;
lblCallState: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
//电话服务
PhoneDialerService: IFMXPhoneDialerService;
{ Private declarations }
public
//需要覆盖
constructor Create(AOwner: TComponent); override;
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} { TForm1 }
//获取电话服务
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));
end; //打电话功能
procedure TForm1.Button2Click(Sender: TObject);
begin
{ test whether the PhoneDialer services are supported on your device }
if Assigned(PhoneDialerService) then
begin
{ if the Telephone Number is entered in the edit box then make the call, else
display an error message }
if edtTelephoneNumber.Text <> '' then
PhoneDialerService.Call(edtTelephoneNumber.Text)
else
begin
ShowMessage('Please type-in a telephone number.');
edtTelephoneNumber.SetFocus;
end;
end;
end; end.

3.检测呼叫状态的变化

 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, FMX.Edit,
FMX.Platform,//需要引入
FMX.PhoneDialer;//需要引入 type
TForm1 = class(TForm)
Button1: TButton;
lblCarrierName: TEdit;
lblISOCountryCode: TEdit;
Button2: TButton;
edtTelephoneNumber: TEdit;
lblCallState: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
//电话服务
PhoneDialerService: IFMXPhoneDialerService;
//定义方法
procedure MyOnCallStateChanged(const ACallID: String; const ACallState: TCallState);
{ Private declarations }
public
//需要覆盖
constructor Create(AOwner: TComponent); override;
{ Public declarations }
end; var
Form1: TForm1; implementation {$R *.fmx}
{$R *.NmXhdpiPh.fmx ANDROID} { TForm1 } //获取电话服务
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
TPlatformServices.Current.SupportsPlatformService(IFMXPhoneDialerService, IInterface(PhoneDialerService));
if Assigned(PhoneDialerService) then
PhoneDialerService.OnCallStateChanged := MyOnCallStateChanged;
end; //检测呼叫状态的变化
procedure TForm1.MyOnCallStateChanged(const ACallID: String;
const ACallState: TCallState);
var outText: String;
begin
case ACallState of
FMX.PhoneDialer.TCallState.None: outText := 'No calls';
FMX.PhoneDialer.TCallState.Connected: outText := 'Connected';
FMX.PhoneDialer.TCallState.Incoming: outText := 'Incoming call';
FMX.PhoneDialer.TCallState.Dialing: outText := 'Dialing';
FMX.PhoneDialer.TCallState.Disconnected: outText := 'Disconnected';
end;
lblCallState.Text := outText;
end; end.

PS:

的 IFMXPhoneDialerService 接口提供了 OnCallStateChanged 事件,允许您处理呼叫状态的变化。 的 TCallState 枚举描述可能的电话状态。

下面的表描述的物品 TCallState 枚举(为每个平台支持的州是标有“+”)。 

描述 iOS 安卓
csNone 没有呼叫状态。 + - - - - - -
csConnected 电话调用者与被调用方。 + +
csIncoming 传入的电话。 + +
csDialing 手机在拨号状态。 + - - - - - -
csDisconnected 调用断开连接。 +