12

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?

Community
  • 1
  • 1
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437

2 Answers2

17

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();
}
Pratik Butani
  • 60,504
  • 58
  • 273
  • 437
Ankit
  • 483
  • 3
  • 8
  • 1
    `tMgr.getSubscriberId();` is for what? – Pratik Butani Feb 01 '14 at 12:26
  • `Settings -> About Phone -> Status -> My phone Number` is `Unkonwn` – Pratik Butani Feb 01 '14 at 12:28
  • 2
    Thanks for that but i want phone number. – Pratik Butani Feb 01 '14 at 12:31
  • It means your phone does not have stored any phone number which i have defined above.. so u can try tMgr.getSubscriberId(); which Returns the unique subscriber ID, for example, the IMSI for a GSM phone. – Ankit Feb 01 '14 at 12:33
  • 1
    or u can get it from 4 down vote accepted I think Sim serial Number is unique. You can use this also. String getSimSerialNumber = tMgr.getSimSerialNumber(); – Ankit Feb 01 '14 at 12:34
  • I am using a Coolpad smartphone with Android 5.1 (API 22). None of the methods above, (including @Ankit's suggestion of `getSimSerialNumber()`) are getting its phone number. Also, there isn't a `My phone number` setting in the `About Phone` settings, as @PratikButani pointed out. My 4G SIM card is in slot 2. Any more sure-shot techniques to get the number? – Narayana J May 17 '16 at 09:13
-1

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" />
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
Hardik Mehta
  • 2,195
  • 1
  • 11
  • 14