■ 전화를 거는 방법을 보여준다.
[TestProject 프로젝트]
▶ IDialer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
namespace TestProject { /// <summary> /// 다이얼러 인터페이스 /// </summary> public interface IDialer { //////////////////////////////////////////////////////////////////////////////////////////////////// Method #region 전화 걸기 - Dial(phoneNumber) /// <summary> /// 전화 걸기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>처리 결과</returns> bool Dial(string phoneNumber); #endregion } } |
▶ PhoneHelper.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 |
using System.Text; namespace TestProject { /// <summary> /// 전화 헬퍼 /// </summary> public static class PhoneHelper { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 숫자 배열 /// </summary> private static readonly string[] _digitArray = { "ABC", "DEF", "GHI", "JKL", "MNO", "PQRS", "TUV", "WXYZ" }; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 전화 번호 구하기 - GetPhoneNumber(source) /// <summary> /// 전화 번호 구하기 /// </summary> /// <param name="source">소스 문자열</param> /// <returns>전화 번호</returns> public static string GetPhoneNumber(string source) { if(string.IsNullOrWhiteSpace(source)) { return null; } source = source.ToUpperInvariant(); StringBuilder stringBuilder = new StringBuilder(); foreach(char character in source) { if (" -0123456789".Contains(character)) { stringBuilder.Append(character); } else { int? result = GetNumber(character); if(result != null) { stringBuilder.Append(result); } else { return null; } } } return stringBuilder.ToString(); } #endregion //////////////////////////////////////////////////////////////////////////////// Private #region 포함 여부 구하기 - Contains(keyString, character) /// <summary> /// 포함 여부 구하기 /// </summary> /// <param name="keyString">키 문자열</param> /// <param name="character">문자</param> /// <returns>포함 여부</returns> private static bool Contains(this string keyString, char character) { return keyString.IndexOf(character) >= 0; } #endregion #region 번호 구하기 - GetNumber(character) /// <summary> /// 번호 구하기 /// </summary> /// <param name="character">문자</param> /// <returns>번호</returns> private static int? GetNumber(char character) { for(int i = 0; i < _digitArray.Length; i++) { if(_digitArray[i].Contains(character)) { return 2 + i; } } return null; } #endregion } } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?xml version="1.0" encoding="UTF-8"?> <ContentPage xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestProject.MainPage"> <ContentPage.Padding> <OnPlatform x:TypeArguments="Thickness"> <On Platform="iOS" Value="20 40 20 20" /> <On Platform="Android,UWP" Value="20" /> </OnPlatform> </ContentPage.Padding> <StackLayout> <Label Text="전화 번호 텍스트를 입력해 주시기 바랍니다 :" /> <Entry x:Name="phoneNumberText" Text="1-855-XAMARIN" /> <Button x:Name="translateButon" Text="번역하기" Clicked="translateButon_Clicked" /> <Button x:Name="callButton" Text="호출하기" IsEnabled="false" Clicked="callButton_Clicked" /> </StackLayout> </ContentPage> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using Xamarin.Forms; namespace TestProject { /// <summary> /// 메인 페이지 /// </summary> public partial class MainPage : ContentPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 번역 번호 /// </summary> private string translateNumber; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 번역하기 버튼 클릭시 처리하기 - translateButon_Clicked(sender, e) /// <summary> /// 번역하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void translateButon_Clicked(object sender, EventArgs e) { this.translateNumber = PhoneHelper.GetPhoneNumber(this.phoneNumberText.Text); if(!string.IsNullOrWhiteSpace(this.translateNumber)) { this.callButton.IsEnabled = true; this.callButton.Text = translateNumber + " 전화걸기"; } else { this.callButton.IsEnabled = false; this.callButton.Text = "전화걸기"; } } #endregion #region 전화걸기 버튼 클릭시 처리하기 - callButton_Clicked(sender, e) /// <summary> /// 전화걸기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private async void callButton_Clicked(object sender, EventArgs e) { bool result = await this.DisplayAlert ( "전화걸기", string.Format("{0} 번호로 전화를 걸으시겠습니까?", this.translateNumber), "예", "아니오" ); if(result) { IDialer dialer = DependencyService.Get<IDialer>(); if(dialer != null) { dialer.Dial(this.translateNumber); } } } #endregion } } |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 |
<?xml version="1.0" encoding="utf-8" ?> <Application xmlns="http://xamarin.com/schemas/2014/forms" xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" x:Class="TestProject.MainApplication"> </Application> |
▶ MainApplication.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation(XamlCompilationOptions.Compile)] namespace TestProject { /// <summary> /// 메인 애플리케이션 /// </summary> public partial class MainApplication : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainApplication() /// <summary> /// 생성자 /// </summary> public MainApplication() { InitializeComponent(); MainPage = new MainPage(); } #endregion } } |
[TestProject.Android 프로젝트]
▶ Dialer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
using System.Linq; using Xamarin.Forms; using Android.Content; using Android.Content.PM; using Android.Telephony; using TestProject.Droid; [assembly: Dependency(typeof(Dialer))] namespace TestProject.Droid { /// <summary> /// 다이얼러 /// </summary> public class Dialer : IDialer { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Public #region 인텐트 이용 가능 여부 구하기 - IsIntentAvailable(context, intent) /// <summary> /// 인텐트 이용 가능 여부 구하기 /// </summary> /// <param name="context">컨텍스트</param> /// <param name="intent">인텐트</param> /// <returns>인텐트 이용 가능 여부</returns> public static bool IsIntentAvailable(Context context, Intent intent) { PackageManager packageManager = context.PackageManager; var list = packageManager.QueryIntentServices (intent, 0).Union(packageManager.QueryIntentActivities(intent, 0)); if(list.Any()) { return true; } TelephonyManager manager = TelephonyManager.FromContext(context); return manager.PhoneType != PhoneType.None; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Instance //////////////////////////////////////////////////////////////////////////////// Public #region 전화 걸기 - Dial(phoneNumber) /// <summary> /// 전화 걸기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>처리 결과</returns> public bool Dial(string phoneNumber) { MainActivity context = MainActivity.Instance; if(context == null) { return false; } Intent intent = new Intent(Intent.ActionCall); intent.SetData(Android.Net.Uri.Parse("tel:" + phoneNumber)); if(IsIntentAvailable(context, intent)) { context.StartActivity (intent); return true; } return false; } #endregion } } |
▶ MainActivity.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
using Android.App; using Android.Content.PM; using Android.OS; namespace TestProject.Droid { /// <summary> /// 메인 액티비티 /// </summary> [Activity(Label = "TestProject", Icon = "@drawable/icon", Theme = "@style/MainTheme", MainLauncher = true, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)] public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Internal #region 인스턴스 - Instance /// <summary> /// 인스턴스 /// </summary> internal static MainActivity Instance { get; private set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Protected #region 생성시 처리하기 - OnCreate(bundle) /// <summary> /// 생성시 처리하기 /// </summary> /// <param name="bundle">번들</param> protected override void OnCreate(Bundle bundle) { TabLayoutResource = Resource.Layout.Tabbar; ToolbarResource = Resource.Layout.Toolbar; base.OnCreate(bundle); Instance = this; global::Xamarin.Forms.Forms.Init(this, bundle); LoadApplication(new MainApplication()); } #endregion } } |
[TestProject.iOS 프로젝트]
▶ Dialer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
using Xamarin.Forms; using Foundation; using UIKit; using TestProject.iOS; [assembly: Dependency(typeof(Dialer))] namespace TestProject.iOS { /// <summary> /// 다이얼러 /// </summary> public class Dialer : IDialer { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 전화 걸기 - Dial(phoneNumber) /// <summary> /// 전화 걸기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>처리 결과</returns> public bool Dial(string phoneNumber) { return UIApplication.SharedApplication.OpenUrl(new NSUrl ("tel:" + phoneNumber)); } #endregion } } |
▶ MainApplicationDelegate.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
using Foundation; using UIKit; namespace TestProject.iOS { /// <summary> /// 메인 애플리케이션 대리자 /// </summary> [Register("MainApplicationDelegate")] public partial class MainApplicationDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 런칭 완료시 처리하기 - FinishedLaunching(application, optionDictionary) /// <summary> /// 런칭 완료시 처리하기 /// </summary> /// <param name="application">애플리케이션</param> /// <param name="optionDictionary">옵션 딕셔너리</param> /// <returns>처리 결과</returns> public override bool FinishedLaunching(UIApplication application, NSDictionary optionDictionary) { global::Xamarin.Forms.Forms.Init(); LoadApplication(new MainApplication()); return base.FinishedLaunching(application, optionDictionary); } #endregion } } |
▶ Program.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
using UIKit; namespace TestProject.iOS { /// <summary> /// 프로그램 /// </summary> public class Program { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Static //////////////////////////////////////////////////////////////////////////////// Private #region 프로그램 시작하기 - Main(argumentArray) /// <summary> /// 프로그램 시작하기 /// </summary> /// <param name="argumentArray">인자 배열</param> private static void Main(string[] argumentArray) { UIApplication.Main(argumentArray, null, "MainApplicationDelegate"); } #endregion } } |
[TestProject.UWP 프로젝트]
▶ Dialer.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
using System; using System.Threading.Tasks; using Windows.ApplicationModel.Calls; using Windows.UI.Popups; using Xamarin.Forms; using TestProject.UWP; [assembly: Dependency(typeof(Dialer))] namespace TestProject.UWP { /// <summary> /// 다이얼러 /// </summary> public class Dialer : IDialer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 전화 걸기 여부 /// </summary> private bool dialled = false; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 전화 걸기 - Dial(phoneNumber) /// <summary> /// 전화 걸기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>처리 결과</returns> public bool Dial(string phoneNumber) { Call(phoneNumber); return dialled; } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 호출하기 - Call(phoneNumber) /// <summary> /// 호출하기 /// </summary> /// <param name="phoneNumber">전화 번호</param> /// <returns>태스크</returns> private async Task Call(string phoneNumber) { PhoneLine phoneLine = await GetDefaultPhoneLineAsync(); if(phoneLine != null) { phoneLine.Dial(phoneNumber, phoneNumber); dialled = true; } else { MessageDialog dialog = new MessageDialog("전화를 걸 수 없습니다."); await dialog.ShowAsync(); dialled = false; } } #endregion #region 디폴트 전화선 구하기 - GetDefaultPhoneLineAsync() /// <summary> /// 디폴트 전화선 구하기 /// </summary> /// <returns>전화선 태스크</returns> private async Task<PhoneLine> GetDefaultPhoneLineAsync() { PhoneCallStore phoneCallStore = await PhoneCallManager.RequestStoreAsync(); Guid lineGUID = await phoneCallStore.GetDefaultLineAsync(); return await PhoneLine.FromIdAsync(lineGUID); } #endregion } } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<forms:WindowsPage x:Class="TestProject.UWP.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:forms="using:Xamarin.Forms.Platform.UWP" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"> <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" /> </forms:WindowsPage> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
namespace TestProject.UWP { /// <summary> /// 메인 페이지 /// </summary> public sealed partial class MainPage { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainPage() /// <summary> /// 생성자 /// </summary> public MainPage() { this.InitializeComponent(); LoadApplication(new TestProject.MainApplication()); } #endregion } } |
▶ App.xaml
1 2 3 4 5 6 7 |
<Application x:Class="TestProject.UWP.App" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" RequestedTheme="Light"> </Application> |
▶ App.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
using System; using Windows.ApplicationModel; using Windows.ApplicationModel.Activation; using Windows.UI.Xaml; using Windows.UI.Xaml.Controls; using Windows.UI.Xaml.Navigation; namespace TestProject.UWP { /// <summary> /// 앱 /// </summary> sealed partial class App : Application { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - App() /// <summary> /// 생성자 /// </summary> public App() { this.InitializeComponent(); this.Suspending += Application_Suspending; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 실행시 처리하기 - OnLaunched(e) /// <summary> /// 실행시 처리하기 /// </summary> /// <param name="e">이벤트 인자</param> protected override void OnLaunched(LaunchActivatedEventArgs e) { Frame rootFrame = Window.Current.Content as Frame; if(rootFrame == null) { rootFrame = new Frame(); rootFrame.NavigationFailed += rootFrame_NavigationFailed; Xamarin.Forms.Forms.Init(e); Window.Current.Content = rootFrame; } if(rootFrame.Content == null) { rootFrame.Navigate(typeof(MainPage), e.Arguments); } Window.Current.Activate(); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 애플리케이션 일시 중지시 처리하기 - Application_Suspending(sender, e) /// <summary> /// 애플리케이션 일시 중지시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Application_Suspending(object sender, SuspendingEventArgs e) { SuspendingDeferral deferral = e.SuspendingOperation.GetDeferral(); deferral.Complete(); } #endregion #region 루트 프레임 네비게이션 실패시 처리하기 - rootFrame_NavigationFailed(sender, e) /// <summary> /// 루트 프레임 네비게이션 실패시 처리하기 /// </summary> /// <param name="sender">이벤트 인자</param> /// <param name="e">이벤트 인자</param> private void rootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) { throw new Exception(string.Format("페이지 로드시 실패하였습니다 : {0}", e.SourcePageType.FullName)); } #endregion } } |