0

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;
milenjao
  • 93
  • 5
  • 18

1 Answers1

0

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;
blong
  • 2,145
  • 13
  • 23
  • To get access to the numbers on any SIMs you'd need to translate code like this: http://stackoverflow.com/a/37292604/2817399 – blong Sep 29 '16 at 13:34
  • Something missing in USES: Undeclared identifier: 'TJContext' – milenjao Sep 29 '16 at 13:35
  • Modified uses clause – blong Sep 29 '16 at 13:38
  • Now it is compilable and runable but it return only empty string. – milenjao Sep 29 '16 at 13:51
  • But other functions form class TelephonyManager gives some results, where can be problem ? I need reliable function to get phone number , because account in my application is fixed to user phone number – milenjao Sep 29 '16 at 14:19
  • I'm not adept in this area - I just follow the documentation. It certainly works on my device. The other code I linked to in the comment above *might* work better, but it is for Lollipop (Android 5.1) and later only. It might be worth trying though. – blong Sep 30 '16 at 21:38
  • I've updated the answer to contain code that uses the Android 5.1 subscription manager. Both versions of the code now return my device telephone number successfully. – blong Sep 30 '16 at 21:55
  • I tested it on Android 6.0 and returning me empty string ? Is possible that could be blocked by mobile operator ? – milenjao Oct 01 '16 at 06:15