6

i have developped an android application, but i need to get the phone number of the phone owner. I have seen many questions here in stackoverflow and many answers but none works for me.

I work with Android 2.2:

I have tested this code, but nothing happened:

  TelephonyManager phoneManager = (TelephonyManager) 
                getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
            String phoneNumber = phoneManager.getLine1Number();

I added the permission in my manifest file READ_PHONE_STATE , in my setting i can't see the phone number :/

I have seen those answers link and this ,and this is my problem

enter image description here

I know that i can get an unique id using getDeviceId() but i'd get the number phone if it's possible .. Can you give more information? Any idea to get the phone number??

EDIT : In my country, we can see our number with USSD command ,the result is in dialogBox, can i read the datas in the USSD response?

I have tested this code, but nothing to get:

@Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            String encodedHash = Uri.encode("#");
            String ussd = "*141*1" + encodedHash;
            startActivityForResult(new Intent("android.intent.action.CALL",
                    Uri.parse("tel:" + ussd)), 1);

        }
    });

.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    Toast.makeText(getApplicationContext(),
            "USSD: " + requestCode + "  " + resultCode + " ", 1).show();

    if (requestCode == 1) {

        if (resultCode == RESULT_OK) {
            // String result=data.getStringExtra("result");
            String dd = data.toString();
            Toast.makeText(getApplicationContext(), dd, 1).show();
        }

    }

I also tested some examples but the result is :

            Android doesn't support USSD service

Help plz?

Community
  • 1
  • 1
Zied R.
  • 4,964
  • 2
  • 36
  • 67
  • on what kind of device are you testing? – peshkira Mar 08 '14 at 18:04
  • What you get when you use `.getLine1Number` ? null? Empty string? You test in in a Emulator or a Real device? – Marco Acierno Mar 08 '14 at 18:10
  • I have Tested in a real device (Samsung Galaxy Pocket) , i get empty string because it's unvailable – Zied R. Mar 08 '14 at 18:12
  • I don't think exist a real solution to it (the same code return my old phone number, so i think it's not the best way to get it.) I found this answer http://stackoverflow.com/a/17121105/1091466 which rely on Whatsapp.. sounds like the best way to get the phone number in most devices. – Marco Acierno Mar 08 '14 at 18:22
  • yes i saw this answer, but i don't think that all users have whatsapp installed. IN my country there is an USSD command that returns the phone number in dialogBox , can i use this way ? and get the repsonse ? – Zied R. Mar 08 '14 at 18:25
  • 1
    USSD interaction is removed since Android 4.2.2. You can find the ticket issue [here](http://code.google.com/p/android/issues/detail?id=57120) – Andrew T. Mar 08 '14 at 18:45
  • You cannot reliably get the number. Some providers issue SIMs without a number so the line1number field cannot be populated. You have to handle this possibility and request the number from the user and save it. My SIM for instance did not get a number associated with it until I registered it online so it does not know its own phone number – patthoyts Mar 08 '14 at 23:04

4 Answers4

6

As you have found, via the many questions and answers I guess you've looked at already, there is no reliable way to get this information:

  • TelephonyManager.getLine1Number() normally returns null, but on the rare phone that it actually returns something else, the answer cannot be relied upon since it returns the original phone number from the SIM, which due to phone number portability is rarely the phone number that the phone is currently using

  • The hack to look for the account name used by WhatsApp is an interesting approach, if WhatsApp is installed. Unfortunately this has now stopped working - WhatsApp no longer creates the Android account using the verified phone number as the account name.

  • Many apps that need this info resort to doing a round trip SMS exchange, since the outgoing SMS will carry the phone number. (Though if you do this, note that many people object to paying the cost of an SMS message, which may well be an international SMS at a premium rate, so will likely give you a bad review in the Play store).

Be aware that although not common in Western markets, there are an increasing number of Dual SIM Android phones (including the new Nokia X), so there is not a 1:1 correspondence between phone and phone number. You also have the problem if your app runs on a tablet that these don't have a phone number anyway.

Community
  • 1
  • 1
zmarties
  • 4,809
  • 22
  • 39
3

I don't think that would be possible.

Alternate solution: This may not be a completely robust solution but i think should work. A user would always have several accounts added e.g facebook, google, twitter etc. Prompt the user to allow sharing personal information and fetch the number from there.

Viral Patel
  • 32,418
  • 18
  • 82
  • 110
1

I did encounter same issue. It's service provider dependent. So with some SIMs it may or may not return self phone number.

Jayesh Tembhekar
  • 516
  • 7
  • 13
0

Try following code for getting mobile number of device.

TelephonyManager mTelephonyMgr =
(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);

String sDeviceID = mTelephonyMgr.getDeviceId();
String sSimSerial = mTelephonyMgr.getSimSerialNumber(); 

Set the following permission

android.permission.READ_PHONE_STATE in Android Manifest file

You can also download my working copy. i.e. Getting registred email id from android device. i hope you will get inspiration from that.

Ankur
  • 24
  • 2