getLine1Number() Returns blank, not null [duplicate]

家住魔仙堡 提交于 2019-11-27 06:45:41

问题


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

OUTPUT in Logcat:

Without Simcard Phones Returns:

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

With Simcard:

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

So what should i do? Is there any Mistake?


回答1:


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:

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



回答2:


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" />


来源:https://stackoverflow.com/questions/21497766/getline1number-returns-blank-not-null

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!