Neste artigo veremos como realizar chamadas telefônicas em suas aplicações Xamarin.Forms.
ADICIONANDO O NUGET PACKAGE
Clique com o botão direito em cima de sua Solution e selecione “Manage NuGet Packages for Solution…”.
Digite “Xam.Plugins.Messaging” e selecione o plugin como demonstrado na imagem a seguir.
Selecione todos os projetos e clique no botão “Install”.
Xaml
Crie um Entry para o usuário informar o número de telefone e um Button para chamar o método Call.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8" ?> | |
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" | |
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | |
xmlns:local="clr-namespace:DemoPhoneCall" | |
x:Class="DemoPhoneCall.MainPage"> | |
<StackLayout VerticalOptions="CenterAndExpand"> | |
<Entry x:Name="Number" Keyboard="Telephone"/> | |
<Button Text="Call" Clicked="Call"/> | |
</StackLayout> | |
</ContentPage> |
C#
Referencie o plugin Messaging e crie um “CrossMessaging.Current.PhoneDialer”, utilize os métodos CanMakePhoneCall para verificar se o dispositivo suporta realizar chamadas e MakePhoneCall para realizar a chamada.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using Plugin.Messaging; | |
using Xamarin.Forms; | |
namespace DemoPhoneCall | |
{ | |
public partial class MainPage : ContentPage | |
{ | |
public MainPage() | |
{ | |
InitializeComponent(); | |
} | |
public void Call(object sender, EventArgs e) | |
{ | |
var phoneDialer = CrossMessaging.Current.PhoneDialer; | |
if (phoneDialer.CanMakePhoneCall && !String.IsNullOrWhiteSpace(Number.Text)) | |
phoneDialer.MakePhoneCall(Number.Text); | |
} | |
} | |
} |
Android
Edite o arquivo AndroidManifest.xml e adicione o provider e meta-data como demonstrado a seguir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<manifest xmlns:android="http://schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="com.companyname.DemoPhoneCall"> | |
<uses-sdk android:minSdkVersion="15" /> | |
<application android:label="DemoPhoneCall.Android"> | |
<provider android:name="android.support.v4.content.FileProvider" | |
android:authorities="com.companyname.DemoPhoneCall.fileprovider" | |
android:exported="false" | |
android:grantUriPermissions="true"> | |
<meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths"></meta-data> | |
</provider> | |
</application> | |
</manifest> |
Observação: Lembre-se de substituir o texto “com.companyname.DemoPhoneCall” pelo nome do pacote da sua aplicação.
Dentro da pasta “Resources” crie uma pasta chamada xml e adicione um arquivo chamado file_paths.xml, como demonstrado a seguir.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<paths xmlns:android="http://schemas.android.com/apk/res/android"> | |
<external-path name="external_files" path="." /> | |
</paths> |
Auto Discagem (Android)
Por padrão o plugin apenas mostra o discador com o número preenchido, se você deseja que o plugin já realize a chamada automaticamente é necessário:
1 – Adicione permissão no AndroidManifest
2 – No arquivo MainActivity.cs do projeto android, antes da inicialização do Xamarin.Forms atribua true para o AutoDial, como demonstrado a seguir.
Resultado
Esse e todos os exemplos deste blog encontram-se disponíveis no GitHub.
oi
muito obrigado pelo seu artigo
Como saber se a ligação foi feita?
ou se o número estiver ocupado
CurtirCurtir
Olá,
A ideia do plugin é realizar a chamada, depois que a chamada é “discada”, fica por conta do usuário conversar na chamada ou identificar se está ocupado.
Espero ter ajudado, abraço.
CurtirCurtir