Android getLine1Number() 返回空白,不为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21497766/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:40:07  来源:igfitidea点击:

getLine1Number() Returns blank, not null

androidtelephonymanager

提问by Pratik Butani

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

我想获取设备的手机号码。我使用了Alex Volovoy 的This Link 的以下代码参考

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

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

OUTPUT in Logcat:

Logcat中的输出:

Without Simcard Phones Returns:

没有 Simcard 手机退货:

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

With Simcard:

使用 SIM 卡:

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

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

我也采取了许可,<uses-permission android:name="android.permission.READ_PHONE_STATE"/>AndroidManifest.xml

So what should i do? Is there any Mistake?

所以我该怎么做?有错误吗?

回答by Ankit

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

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

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:

每个运营商的 SIM 卡上都没有电话号码,就像在印度 Sim 在任何内存中都没有电话号码,所以我们无法从这些连接中获取电话号码。但是,某些国家/地区和运营商已将电话号码存储在 SIM 卡上,我们可以获取这些号码。为了使其适用于所有设备,我们可以采用两种策略:

To avoid this problem , we can catch the error and work accordingly. Like:

为了避免这个问题,我们可以捕获错误并进行相应的工作。喜欢:

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();
}

回答by Hardik Mehta

I have another solution to get a phone number when telephony manager returns blank. I hope it'll help you.

当电话经理返回空白时,我有另一个解决方案来获取电话号码。我希望它会帮助你。

Here is my sample code:

这是我的示例代码:

public static final String main_data[] = {
            "data1", "is_primary", "data3", "data2", "data1", "is_primary", "photo_uri", "mimetype"
    };


        object object = (TelephonyManager) context.getSystemService("phone");
        if (!TextUtils.isEmpty(((TelephonyManager) (object)).getLine1Number())) {

        }
        object = context.getContentResolver().query(Uri.withAppendedPath(android.provider.ContactsContract.Profile.CONTENT_URI, "data"), main_data, "mimetype=?", new String[]{
                "vnd.android.cursor.item/phone_v2"
        }, "is_primary DESC");
        if (object != null) {
            do {
                if (!((Cursor) (object)).moveToNext()) {
                    break;
                }

             if (s.equals("vnd.android.cursor.item/phone_v2")) {
                    String s1 = ((Cursor) (object)).getString(4);
                    boolean flag1;
                    if (((Cursor) (object)).getInt(5) > 0) {
                        flag1 = true;
                    } else {
                        flag1 = false;
                    }
                    Toast.makeText(SampleActivity.this, "Phone:-" + s1, Toast.LENGTH_LONG).show();
                }
            } while (true);
            ((Cursor) (object)).close();
        }

Also don't forget to add these two permissions:

另外不要忘记添加这两个权限:

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