36

I have to make an application which shows the contact no of the SIM card that is being used in the cell. For that I need to use TelephonyManager class. Can I get details on its usage?

CopsOnRoad
  • 237,138
  • 77
  • 654
  • 440
Narasimha
  • 3,802
  • 11
  • 55
  • 83
  • 3
    You really should try to explain your question a lot better. Your question is hard to understand and lacks any details that would allow anyone to help you. – Donal Rafferty Feb 05 '10 at 10:52
  • Are you trying to get the mobile number of device in to your Android application? – slhsen Feb 05 '10 at 11:07
  • Hi Narasimha, I want 10 digit phone number, tm.getLine1Number() returns null. Can you please share your solution ? – Gangadhar Nimballi Mar 28 '14 at 10:27
  • Weird. When I click 'status' I get a different phone umber to my own one. I think this may be because I have been using the same mobile/ cell forever. But I have switched network provider. So the new network provider has done something like ported my number, which is 087, to their system, which is 085 prefix. The status on my phone says my phone number is 085 XXXXXX, but this number means nothing to me or anyone who knows me. – CHarris Jan 02 '15 at 22:34

6 Answers6

66

You can use the TelephonyManager to do this:

TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE); 
String number = tm.getLine1Number();

The documentation for getLine1Number() says this method will return null if the number is "unavailable", but it does not say when the number might be unavailable.

You'll need to give your application permission to make this query by adding the following to your Manifest:

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

(You shouldn't use TelephonyManager.getDefault() to get the TelephonyManager as that is a private undocumented API call and may change in future.)

Sudip Bhandari
  • 2,165
  • 1
  • 27
  • 26
David Webb
  • 190,537
  • 57
  • 313
  • 299
6

Hi Actually this is my same question but I didn't get anything.Now I got mobile number and his email-Id from particular Android real device(Android Mobile).Now a days 90% people using what's App application on Android Mobile.And now I am getting Mobile no and email-ID Through this What's app API.Its very simple to use see this below code.

            AccountManager am = AccountManager.get(this);
            Account[] accounts = am.getAccounts();
      for (Account ac : accounts) 
       {
        acname = ac.name;

        if (acname.startsWith("91")) {
            mobile_no = acname;
        }else if(acname.endsWith("@gmail.com")||acname.endsWith("@yahoo.com")||acname.endsWith("@hotmail.com")){
            email = acname;
        }

        // Take your time to look at all available accounts
        Log.i("Accounts : ", "Accounts : " + acname);
    }

and import this API

    import android.accounts.Account;
    import android.accounts.AccountManager;
Amarnath Baitha
  • 561
  • 1
  • 6
  • 19
  • 2
    why are you comparing `acname` with `91`. Will it work only in india – sarabu Jan 06 '14 at 06:57
  • @Amarnath Baitha In WhatsApp U can use the any sim number witch one not inserted on this device where WhatsApp is installed?? that's time what is the right solution? It's never mandatory in What'SApp that simcard is require!! – Zala Janaksinh Oct 08 '14 at 08:49
3

I have to make an application which shows the Contact no of the SIM card that is being used in the cell. For that I need to use Telephony Manager class. Can i get details on its usage?

Yes, You have to use Telephony Manager;If at all you not found the contact no. of user; You can get Sim Serial Number of Sim Card and Imei No. of Android Device by using the same Telephony Manager Class...

Add permission:

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

Import:

import android.telephony.TelephonyManager;

Use the below code:

TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);

         // get IMEI
         imei = tm.getDeviceId();

         // get SimSerialNumber
         simSerialNumber = tm.getSimSerialNumber();
Yasar Khan
  • 71
  • 12
1

Well, all could be temporary hacks, but there is no way to get mobile number of a user. It is against ethical policy.

For eg, one of the answers above suggests getting all accounts and extracting from there. And it doesn't work anymore! All of these are hacks only.

Only way to get user's mobile number is going through operator. If you have a tie-up with mobile operators like Aitel, Vodafone, etc, you can get user's mobile number in header of request from mobile handset when connected via mobile network internet.

Not sure if any manufacturer tie ups to get specific permissions can help - not explored this area, but nothing documented atleast.

Amit
  • 124
  • 10
0

Sometimes you can retreive the phonenumber with a USSD request to your operator. For example I can get my phonenumber by dialing *116# This can probably be done within an app, I guess, if the USSD responce somehow could be catched. Offcourse this is not a method I would recommend to use within an app that is to be distributed, the code may even differ between operators.

ziggestardust
  • 211
  • 2
  • 13
  • This is not working for all the operators in all the countries. – Vinothkumar Arputharaj Apr 10 '12 at 08:48
  • Thats correct, and thats also what I wrote in my comment "sometimes you can retreive the phonenumber ...". In some cases they might use a different USSD code and Sometimes they might not have the feature at all. – ziggestardust Apr 21 '12 at 14:29
  • USSD response can be retrieved using `AccessibilityService`. This works for most operators I think. But then, you would need to access Accessibility permission which is considered as dangerous permission. – cgb_pandey Mar 08 '23 at 11:23
-2

As many said:

String phoneNumber = TelephonyManager.getDefault().getLine1Number();

The availability depends strictly on the carrier and the way the number is encoded on the SIM card. If it is hardcoded by the company that makes the SIMs or by the mobile carrier itself. This returns the same as in Settings->about phone.

Lukas
  • 455
  • 4
  • 11