VINET
VINET

Reputation: 661

Xamarin: How to get a phone number?

I'm writing a test application, on the xamarin for the android. You need to know the phone number. I tried a lot of options. Last:

        [assembly: Xamarin.Forms.Dependency(typeof(PhoneNumberService))]
    public string GetMyPhoneNumber()
    {
        TelephonyManager mgr = Application.Context.GetSystemService(Context.TelephonyService) as TelephonyManager;
        return mgr.Line1Number;
    }

AssemblyInfo.cs:

<`uses-permission android:name="android.permission.READ_PHONE_STATE"/>`

AndroidManifest.xml:

 [assembly: UsesPermission(Android.Manifest.Permission.ReadPhoneState)]

On the emulator works well on the phone does not work. Returns an empty string.

How to get a phone number?

Upvotes: 2

Views: 4363

Answers (1)

mehdi farhadi
mehdi farhadi

Reputation: 1532

To solve these problems,this link might help you

Try that:

            TelephonyManager mTelephonyMgr;

            mTelephonyMgr = (TelephonyManager)GetSystemService(TelephonyService);

            var Number = mTelephonyMgr.Line1Number;

Upvotes: 1

Related Questions