TelephonyManager.getLine1Number()返回null

时间:2017-12-30 16:03:10

标签: java android

mPhoneNumber变量返回null。我已经调试了这个东西一个星期,我非常困难。为什么getLine1Number会返回null?

try
{
    TelephonyManager tMgr = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
    if (tMgr != null)
    {
        if (checkSelfPermission(android.Manifest.permission.READ_SMS) != PackageManager.PERMISSION_GRANTED)
        {
            if (checkSelfPermission(android.Manifest.permission.READ_PHONE_STATE) == PackageManager.PERMISSION_GRANTED)
            {
                // TODO: Consider calling
                // Activity#requestPermissions
                // here to request the missing permissions, and then overriding
                // public void onRequestPermissionsResult(int requestCode, String[] permissions,
                // int[] grantResults)
                // to handle the case where the user grants the permission. See the documentation
                // for Activity#requestPermissions for more details.
                return;
            }
        }
        String mPhoneNumber = tMgr.getLine1Number();
        // Log.v("PHONE: ", mPhoneNumber);
    }
    else
    {
        //Log.v("PHONE NO: ", "NO");
    }   
}
catch(Exception e)
{
    // TODO: Handle exception
}

1 个答案:

答案 0 :(得分:2)

网络提供商并不总是将电话号码(msisdn)存储在SIM卡上。因此,获取电话号码很大程度上取决于您使用的移动运营商。

一些运营商在网关通过后将电话号码放入标题(对于网络浏览器用户);其中一些将msisdn直接放在SIM卡上;其中一些只提供"令牌来识别他们的用户。

这可能是你获得" null"当你试图得到你的号码时的价值。

请检查that