19

is it possible to get the phonenumber of each device programmatically? I tried this code:

TelephonyManager manager =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = manager.getLine1Number();

This works fine with some providers. Unfortunately it does not work with each provider. So i am looking for a trick or work around to get the phonenumber of the device. Is there a solution or is it impossible because the number is not stored on the sim card?

3
  • Hi, Did you get solution to find the phone number will all carriers, Can you please share me your solution ? Thank you Commented Mar 28, 2014 at 5:52
  • I can tell you that it is not possible. Some providers do not store the phonenumber on SIM card. So you might ask the user to enter the phonenumber and verify it with a sms and BroadcastReceiver. This is the only alternative I have found. Commented Mar 28, 2014 at 20:15
  • possible duplicate of Programmatically obtain the phone number of the Android phone
    – bluish
    Commented Dec 9, 2014 at 9:53

3 Answers 3

18

The method you are using is the only one part of the SDK to do this, and only works on devices where the number is stored on the SIM card, which only some carriers do. For all other carriers, you will have to ask the user to enter the phone number manually, as the number is simply not stored anywhere on the device from where you can retrieve it.

3
0

You can try to send specific SMS to ISP. For example, in Beijing(China), when you send SMS "501" to 10001, you will get your phone number in the received message. Then you only need to know how to send SMS and register a BroadcastReceiver to receive the message.

1
  • @Billa Dec yes~~ Commented Feb 2, 2018 at 4:26
0

Now its not require any permission to get phone number use Play Services API without the permission and hacks. Source and Full example.

build.gradle (version 10.2.x and higher required):

compile "com.google.android.gms:play-services-auth:$gms_version"

In your activity (the code is simplified):

enter image description here

@Override
protected void onCreate(Bundle savedInstanceState) {
// ...
googleApiClient = new GoogleApiClient.Builder(this)
        .addApi(Auth.CREDENTIALS_API)
        .build();
requestPhoneNumber(result -> {
    phoneET.setText(result);
});
}

public void requestPhoneNumber(SimpleCallback<String> callback) {
phoneNumberCallback = callback;
HintRequest hintRequest = new HintRequest.Builder()
        .setPhoneNumberIdentifierSupported(true)
        .build();

PendingIntent intent = Auth.CredentialsApi.getHintPickerIntent(googleApiClient, 
hintRequest);
try {
    startIntentSenderForResult(intent.getIntentSender(), PHONE_NUMBER_RC, null, 
0, 0, 0);
} catch (IntentSender.SendIntentException e) {
    Logs.e(TAG, "Could not start hint picker Intent", e);
}
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHONE_NUMBER_RC) {
    if (resultCode == RESULT_OK) {
        Credential cred = data.getParcelableExtra(Credential.EXTRA_KEY);
        if (phoneNumberCallback != null){
            phoneNumberCallback.onSuccess(cred.getId());
        }
    }
    phoneNumberCallback = null;
}
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.