How to get phone number or sim card information use android studio? (simcard 1 or 2)
I have used this code:
TelephonyManager tMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
phone_number.setText(tMgr.getLine1Number());
And I've also added permissions in AndroidManifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
But all the gadgets that I apply fail to get phone number or always generate a null value.
And this is the build.gradle I use:
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
//noinspection GradleCompatible
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
//noinspection GradleCompatible
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
//noinspection UseOfBundledGooglePlayServices
implementation 'com.google.android.gms:play-services:12.0.1'
implementation 'com.google.android.gms:play-services-auth:15.0.1'
}
There is no reliable way to get the phone number from the SIM card. The TelephonyManager reads the phone number from SIM card but its upto the Telecom operators to add this information in the SIM card.
Most of the Telecom operators don't add this information in SIM card hence its not reliable enough.
There is a way to use Google-Play-Service to get the phone number, but it also doesn't gurantee 100% to return the phone number. You can do it as follows.
Add following dependencies in build.gradle
:
dependencies {
...
compile 'com.google.android.gms:play-services:11.6.0'
compile 'com.google.android.gms:play-services-auth:11.6.0'
}
Create two constants in MainActivity.java
:
private static final int PHONE_NUMBER_HINT = 100;
private final int PERMISSION_REQ_CODE = 200;
In onclick
of your Button add following:
final HintRequest hintRequest =
new HintRequest.Builder().setPhoneNumberIdentifierSupported(true).build();
try {
final GoogleApiClient googleApiClient =
new GoogleApiClient.Builder(MainActivity.this).addApi(Auth.CREDENTIALS_API).build();
final PendingIntent pendingIntent =
Auth.CredentialsApi.getHintPickerIntent(googleApiClient, hintRequest);
startIntentSenderForResult(
pendingIntent.getIntentSender(),
PHONE_NUMBER_HINT,
null,
0,
0,
0
);
} catch (Exception e) {
e.printStackTrace();
}
Add onActivityResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PHONE_NUMBER_HINT && resultCode == RESULT_OK) {
Credential credential = data.getParcelableExtra(Credential.EXTRA_KEY);
final String phoneNumber = credential.getId();
}
}
This are few methods you can use:
mTelephonyMgr = (TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String countryiso=mTelephonyMgr.getSimCountryIso();
String simOperator=mTelephonyMgr.getSimOperator();
String simOperatorName=mTelephonyMgr.getSimOperatorName();
String simSerialNo=mTelephonyMgr.getSimSerialNumber();
int simState=mTelephonyMgr.getSimState();
String subscriberID=mTelephonyMgr.getSubscriberId();
Edit
String mPhoneNumber = mTelephonyMgr.getLine1Number();
for more,visit here.
This Permission is also necessary.
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I hope this helps.