DelphiWorlds / Kastri

Cross-platform library for Delphi
MIT License
486 stars 117 forks source link

TJTelecomManager usage #200

Closed EndErr closed 8 months ago

EndErr commented 8 months ago

Can someone show an example of how to use TJTelecomManager? I try to make a call to a number, wait few seconds and terminate the call, also I need to get some status of the call: no answer, answered, invalid number and any types of available statuses.

DelphiWorlds commented 8 months ago

This is how you would obtain an instance of it:

uses
  System.Permissions,
  Androidapi.JNI.JavaTypes, Androidapi.JNI.GraphicsContentViewText, Androidapi.Helpers, Androidapi.JNI.Net, Androidapi.JNI.Os,
  DW.Androidapi.JNI.Telecom;

function GetTelecomManager: JTelecomManager;
var
  LObject: JObject;
begin
  Result := nil;
  LObject := TAndroidHelper.Context.getSystemService(TJContext.JavaClass.TELECOM_SERVICE);
  if LObject <> nil then
    Result := TJTelecomManager.Wrap(LObject);
end;

To place a call:

function Call(const ANumber: string): Boolean;
var
  LManager: JTelecomManager;
  LUri: Jnet_Uri;
begin
  Result := False;
  LManager := GetTelecomManager;
  if LManager <> nil then
  begin
    LUri := TJnet_Uri.JavaClass.Parse(StringToJString('tel:' + ANumber));
    LManager.placeCall(LUri, TJBundle.JavaClass.EMPTY);
  end;
end;

..and call this function this way:

  Call('123456789'); // for example

Note that making calls requires the CALL_PHONE permission, which can be set in the Dangerous Permissions section of the Permissions item of the Project Options. As it is classified as dangerous, you will need to request the permission at runtime, e.g:

procedure RequestCallPermissions;
begin
  PermissionsService.RequestPermissions(['android.permission.CALL_PHONE'],
    procedure(const APermissions: TClassicStringDynArray; const AGrantResults: TClassicPermissionStatusDynArray)
    begin
      if AGrantResults[0] = TPermissionStatus.Granted then
      begin
        // Can now make calls
      end;
    end
  );
end;

I haven't actually tested this code - I am just going by the documentation: https://developer.android.com/reference/android/telecom/TelecomManager

EndErr commented 8 months ago

Thanks for the example.

How to terminate a call after some time? And how to get status of calls (invalid number, no answer from destination, answered, and any other types of statuses)?

DelphiWorlds commented 8 months ago

How to terminate a call after some time?

Use the endCall method, e.g:

  LManager.endCall;

And how to get status of calls (invalid number, no answer from destination, answered, and any other types of statuses)?

The easiest way would be to use Delphi's built-in phone dialer service. The details for handling changes in call state are here: https://docwiki.embarcadero.com/RADStudio/Alexandria/en/Mobile_Tutorial:_Using_the_Phone_Dialer_on_Mobile_Devices_(iOS_and_Android)#Detecting_the_Call_State_Changes

In future, for just asking questions, please use an online forum or chat room as detailed here: https://github.com/DelphiWorlds/HowTo/blob/main/Readme.md e.g. Delphi Praxis etc. You will have a much wider audience of developers to answer your questions

EndErr commented 8 months ago

Thanks for the reply. Please don't get upset about my questions, of course I also used google searches and consulted all kinds of resources. The question regarding the status of the call was to find out if there are other data available besides those exposed in the documentation and the examples in FMX. Now I have answered my questions, for which I thank you.