android specific event in event handler

in the Android section of my solution I have created the following class for getting the phone's own number:

[Service]
        public class PhoneNr : IntentService
        {       
           public string ownNumber;
            protected override void OnHandleIntent(Intent intent)
            {
                var telephonyManager = (TelephonyManager)this.ApplicationContext.GetSystemService(Context.TelephonyService);               
                ownNumber =telephonyManager.Line1Number; 
           }             
        }

On my general main page, I want to execute this class through the event handler of a button, but of course in such way, that it will only activate when the phone is an Android.
So that would be something similar to:

private void Btn_Clicked(object sender, EventArgs e)
        {
            //if phone = android
            PhoneNr p = new PhoneNr();
        label.txt = p.ownNumber;
       }

Anyone who can tell me how to implement this correctly in the button's event handler?
Thanks a lot,

Answers

  • ClintStLaurentClintStLaurent USUniversity ✭✭✭✭✭

    DependencyService
    In general you make an Interface at the agnostic layer. Then platform specific implementation for android, iOS etc.
    Your main code doesn't know or care what OS its running on. So in this case it just knows to get the phone number - and doesn't care how or which implementation exists.

    I have other concerns though from your code and hope its just because you're showing us quick & dirty test code and now how you would do it in a real production app.
    Specifically using .Clicked... and changing the text of a label fro code behind. That's all very 2001 WinForms and not close to how things are done these days (this decade or even this century).

  • JohnHardmanJohnHardman GBUniversity mod
    edited June 10

    @RHoogland

    Define an interface for a dependency service (e.g. ITelephonyService) that includes a method to get the user's own number (remembering that some phones have more than one Simcard and hence more than one number).
    Implement that dependency service for Android, remembering to register the service

    Define a ViewModel that implements INotifyPropertyChanged, and that exposes a command (derived from ICommand) that calls the dependency service method that gets the user's own number, and that then populates a string property in the ViewModel with that retrieved number.
    The string property needs a getter and a setter. When the property changes as a result of the setter being called with a new value, fire the PropertyChanged event

    Bind the Command property of the Button in your main page with the command in the ViewModel
    Bind the Text property of the Label in your main page with the string property

  • yelinzhyelinzh Member, Xamarin Team Xamurai

    @RHoogland
    DependencyService allows apps to call into platform-specific functionality from shared code. This functionality enables Xamarin.Forms apps to do anything that a native app can do.

    Xamarin.Forms apps need four components to use DependencyService:

    • Interface – The required functionality is defined by an interface in shared code.
    • Implementation Per Platform – Classes that implement the interface must be added to each platform project.
    • Registration – Each implementing class must be registered with DependencyService via a metadata attribute. Registration enables DependencyService to find the implementing class and supply it in place of the interface at run time.
    • Call to DependencyService – Shared code needs to explicitly call DependencyService to ask for implementations of the interface.

    Tutorial: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/dependency-service/introduction

Sign In or Register to comment.