0
var tMgr = (TelephonyManager)Forms.Context.ApplicationContext.GetSystemService(Android.Content.Context.TelephonyService);
     return tMgr.Line1Number;

This code is returning null.

I want to generate this type of popup while registration image

  • Where do you call the method ?Could you show more codes about it ?I could get the phone number correctly in MainActivity with the method. – Leo Zhu Feb 18 '21 at 02:27
  • Hello, Please refere this Link https://stackoverflow.com/questions/45068889/xamarin-forms-display-the-phone-number-of-my-sim-card-at-device-on-the-screen . – Krunal Bagadia Feb 18 '21 at 05:58

1 Answers1

0

Firstly , we need to add the permission in AndroidManifest.xml

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

In Addition , Forms.Context is obsolete after XF 2.5 . So we could use the plugin Plugin.CurrentActivity from nuget.

Simply call the Init method on OnCreate in MainActivity

CrossCurrentActivity.Current.Init(this, bundle);

And access it like following

var tMgr = (TelephonyManager)CrossCurrentActivity.Current.AppContext.GetSystemService(Android.Content.Context.TelephonyService);
 return tMgr.Line1Number;
Lucas Zhang
  • 18,630
  • 3
  • 12
  • 22