getLine1Number()返回空,不为空 [英] getLine1Number() Returns blank, not null

查看:3442
本文介绍了getLine1Number()返回空,不为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得设备的手机号码。我已经使用继亚历克斯Volovoy的 This链接

I want to get Mobile Number of Device. I have used following code reference by Alex Volovoy's This Link

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

Log.d("msg", "Phone : "+mPhoneNumber);

输出在logcat中:

02-01 17:22:45.472: D/msg(29102): Phone : null

随着simcard的:

02-01 17:22:45.472: D/msg(29102): Phone : 

我也采取了许可,不得以 &LT;使用-权限的Andr​​oid:名称=android.permission.READ_PHONE_STATE/&GT; 在< STRONG> 的Andr​​oidManifest.xml

I have also taken permission in <uses-permission android:name="android.permission.READ_PHONE_STATE"/> in AndroidManifest.xml

所以,我应该怎么办?有没有搞错?

So what should i do? Is there any Mistake?

推荐答案

要得到的电话号码从设备,首先你要在设备上设置自己的手机号码,只要通过:

To get the phone number from the device , first you have to set your own phone number on the device, just through :

设置 - >关于手机 - >状态 - >我的电话号码

Settings -> About Phone -> Status -> My phone Number

电话号码不可用的SIM卡,每个运营商,像印度辛不要有电话号码的任何内存,因此我们不能得到的电话号码这些连接。然而,一些国家和运营商都存储在SIM卡的电话号码,我们可以得到这些。为了使这项工作的所有设备,我们可以采用两种策略:

Phone numbers are not available on SIM for each operators, like in india Sim dont have phone numbers in any memory, So WE cant get phone number from these connection. However, some countries, and operators have stored phone numbers on SIM, and we can get those. TO make this to work for all devices we can employ two strategies:

为了避免这个问题,我们可以捕捉到错误,并相应工作。像:

TelephonyManager tMgr = (TelephonyManager) 
                 ShowMyLocation.this.getSystemService(Context.TELEPHONY_SERVICE);

String MyPhoneNumber = "0000000000";

try 
{
    MyPhoneNumber =tMgr.getLine1Number();
}
catch(NullPointerException ex)
{
}

if(MyPhoneNumber.equals("")){
    MyPhoneNumber = tMgr.getSubscriberId();
}

这篇关于getLine1Number()返回空,不为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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