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?

有帮助吗?

解决方案

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

其他提示

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" />
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top