簡體   English   中英

TelephonyManager.getLine1Number()返回null

[英]TelephonyManager.getLine1Number() returns null

mPhoneNumber變量返回空值。 我已經調試了一個星期,這讓我非常困惑。 為什么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
}

網絡提供商並不總是將電話號碼(msisdn)存儲在SIM卡上。 因此,獲取電話號碼在很大程度上取決於您使用的移動運營商。

一些運營商在網關通過后將電話號碼放在標題中(對於Web瀏覽器用戶)。 其中一些將msisdn直接放置到SIM卡中; 其中一些僅提供“令牌”來識別其用戶。

這可能是為什么當您嘗試獲取數字時獲得“空”值的原因。

請檢查一下

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM