获取手机Xamarin.Android的号码? [英] Getting the number of the phone Xamarin.Android?

查看:636
本文介绍了获取手机Xamarin.Android的号码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现某些用户共享的代码在我的项目中不起作用.有谁有更好的解决方案?

I found out that this code shared by some users doesn't work in my project. Has anyone a better solution ?

TelephonyManager tMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();

我在getLine1Number()mAppContext

推荐答案

您的代码适用于本机 Android(Java).尝试下面的 Xamarin.Android(c#)代码.

Your code is for native Android (Java). Try the below code of Xamarin.Android (c#).

代码:

Android.Telephony.TelephonyManager tMgr = (Android.Telephony.TelephonyManager)this.GetSystemService(Android.Content.Context.TelephonyService);
string mPhoneNumber = tMgr.Line1Number;

所需权限:

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


[已编辑]

有时Line1Number如果该数字不可用,属性将返回null,但没有说明该数字何时不可用.

Sometimes Line1Number Property will return null if the number is unavailable, but it does not say when the number might be unavailable.

这些是由于电话号码未在您的手机中注册或由于设备制造商创建的某些限制而发生的.

These are occured due to the reason that the phone number not register in your mobile, or according to some restrictions created by the device manufactures.

来自文档:

返回第1行的电话号码字符串,例如GSM电话的MSISDN.如果不可用,则返回null.

因此,您做对了所有事情,但是没有存储电话号码.

So you have done everything right, but there is no phone number stored.

如果您获得null,则可以显示一些内容,以使用户自己输入电话号码.

If you get null, you could display something to get the user to input the phone number on his/her own.

这篇关于获取手机Xamarin.Android的号码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆