Question

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?

Was it helpful?

Solution

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.

OTHER TIPS

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.

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

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top