c# - Xamarin.Droid - Get device phone number -


i'm working on xamarin forms project in need able user's phone number device. figured out not possible on ios , windows phone, still want android, that's why i've found on internet piece of code should working on android. i'm using mvvm pattern.

in viewmodel, try phone number way :

   string phonenumber = dependencyservice.get<iphonenumber>().getphonenumber(); 

which calls method class :

 public class phonenumber : iphonenumber     {         public string getphonenumber()         {             telephonymanager mtelephonymgr;                 mtelephonymgr = (telephonymanager)application.context.getsystemservice(context.telephonyservice);                 return mtelephonymgr.line1number;         }     } 

when run app on android emulator visual studio, works, phone number made of 11 digits. when i'm on real device, method returns empty string (""). i've put read_phone_state permission in androidmanifest.xml file. oh also, of course, real device has sim card plugged in, , has 1 sim slot.

did miss ? i'll try test on device can, not possible.

thanks !

so, emad said in comment of question :

i tried long ago using native android programs , shown many operators don't give information. in iran non of active operators that. i've heard it's illegal! that's why apps telegram , viber ask number.

so, after few more research, indeed not possible phone number sure without asking user.

hope helped !


Comments

Popular posts from this blog

javascript - Clear button on addentry page doesn't work -

c# - Selenium Authentication Popup preventing driver close or quit -

tensorflow when input_data MNIST_data , zlib.error: Error -3 while decompressing: invalid block type -