4

I used in my c# script

Application.OpenURL("tel:+79011111115");

Dialer appeared, but phone call did not occur If it were Java, I could say that it worked like

Intent call = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:+79011111115"));

but I need:

Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+79011111115"));

Is there any analogy of Java's ACTION_CALL in C#?
Thanks in advance

Programmer
  • 121,791
  • 22
  • 236
  • 328
Dmbor
  • 63
  • 1
  • 5

2 Answers2

3

You can make your Java code into .jar or .aar plugin the call it from C#. You can also use Unity's AndroidJavaClass and AndroidJavaObject API which totally eliminates the need for a Java compiled plugin.

With the AndroidJavaClass and AndroidJavaObject API, the equivalent of the Java code below:

Intent call = new Intent(Intent.ACTION_CALL, Uri.parse("tel:+79011111115"));

in C# is as below:

string phoneNum = "tel: +79011111115";

//For accessing static strings(ACTION_CALL) from android.content.Intent
AndroidJavaClass intentStaticClass = new AndroidJavaClass("android.content.Intent");
string actionCall = intentStaticClass.GetStatic<string>("ACTION_CALL");

//Create Uri
AndroidJavaClass uriClass = new AndroidJavaClass("android.net.Uri");
AndroidJavaObject uriObject = uriClass.CallStatic<AndroidJavaObject>("parse", phoneNum);

//Pass ACTION_CALL and Uri.parse to the intent
AndroidJavaObject intent = new AndroidJavaObject("android.content.Intent", actionCall, uriObject);

Remember you must start the Activity on the Intent to finish it and below is what it looks like in Java:

startActivity(call);

Below is the equivalent of that code in C# code to start the Activity:

AndroidJavaClass unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject unityActivity = unityPlayer.GetStatic<AndroidJavaObject>("currentActivity");

try
{
    //Start Activity
    unityActivity.Call("startActivity", intent);
}
catch (Exception e)
{
    Debug.LogWarning("Failed to Dial number: " + e.Message);
}

Finally, just like in Java, you must also add <uses-permission android:name="android.permission.CALL_PHONE"></uses-permission> permission otherwise it won't work. See this post for how to add this Android permission to Unity.

For Android 6.0 and above. You have to use run-time permission. This Github project should be work fine just for this.

PassetCronUs
  • 447
  • 4
  • 11
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Hi I tried your code but open skype call instead of the normal call system is it normal? is meant for for skype call? I was looking for the default system call via sim card. – Francesco Sep 12 '22 at 15:06
0

Use this for android.

Application.OpenURL("tel://[+1234567890]");
Nitin Biltoria
  • 151
  • 1
  • 6