简体   繁体   中英

Delphi FMX Android app Get device phone number

I need get telephone number of device on which my application runing. If has device two SIM cards ideal to get both numbers or if SIM card is not inserted (tablet device) can detect this.

I found some JAVA code but I have no idea how translate it to Delphi

TelephonyManager phneMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String phneNmbr = phneMgr.getLine1Number();

I try write something but it not working ....

USES  Androidapi.Helpers,  Androidapi.JNI.JavaTypes, Androidapi.JNI.Telephony;

procedure TForm1.Button1Click(Sender: TObject);
var
  num: JString;
  tman: Androidapi.JNI.Telephony.JTelephonyManager;
begin
  tman:=TJtelephonyManager.Create;
  num := tman.getLine1Number;
  edit1.Text:=Jstringtostring(num);
end;

Something like this should do it, based on experience with other system services. This translates what you have suggested is viable Java code.

I'll edit this to make it compile correctly (if there are any issues with it) when I have a copy of Delphi to hand later, but this is roughly what is required.

Note that quick look at the telephony manager documentation doesn't readily say how one would get the phone number for a second SIM, but it does translate what you were trying to translate.

uses
  System.SysUtils,
  Androidapi.Helpers,
  Androidapi.JNI.GraphicsContentViewText,
  Androidapi.JNI.JavaTypes,
  Androidapi.JNI.Telephony;

function DeviceTelephoneNumber: string;
var
  TelephonyManagerObj: JObject;
  TelephonyManager: JTelephonyManager;
begin
  TelephonyManagerObj:= TAndroidHelper.Context.getSystemService(
    TJContext.JavaClass.TELEPHONY_SERVICE);
  if TelephonyManagerObj <> nil then
  begin
    TelephonyManager := TJTelephonyManager.Wrap(TelephonyManagerObj);
    if TelephonyManager <> nil then
      Result := JStringToString(TelephonyManager.getLine1Number);
  end;
end;

This code is also a possibility, which works in Android 5.1 and later.

function DeviceTelephoneNumbers: TArray<string>;
var
  SubscriptionManager: JSubscriptionManager;
  I, SubscriptionInfoCount: Integer;
  SubscriptionInfoList: JList;
  SubscriptionInfo: JSubscriptionInfo;
begin
  // Subscription manager is only available in Android 5.1 and later
  if TOSVersion.Check(5, 1) then
  begin
    SubscriptionManager := TJSubscriptionManager.JavaClass.from(
      TAndroidHelper.Context);
    SubscriptionInfoCount := SubscriptionManager.getActiveSubscriptionInfoCount;
    SubscriptionInfoList := SubscriptionManager.getActiveSubscriptionInfoList;
    SetLength(Result, SubscriptionInfoCount);
    for I := 0 to Pred(SubscriptionInfoCount) do
    begin
      SubscriptionInfo := TJSubscriptionInfo.Wrap(SubscriptionInfoList.get(I));
      if SubscriptionInfo <> nil then
        Result[I] := JStringToString(SubscriptionInfo.getNumber);
    end;
  end
  else
  begin
    // If running on older OS, use older API
    SetLength(Result, SubscriptionInfoCount);
    Result[0] := DeviceTelephoneNumber
  end;
end;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM