How to Get the Mobile Number of Current Sim Card in Real Device

How to get the mobile number of current sim card in real device?

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.)

Codenameone:How to get the mobile number of current sim card in real device?

Mobile devices don't provide that information for security/privacy reasons. You can access it in some low level Android API's but since this isn't portable we don't expose that.

Notice that apps like whatsapp, uber, gettaxi etc. all ask you to type in your phone number then a verification code sent via an SMS. That's exactly what JAT (which was built with Codename One) does.

get phone number from sim card with telephonymanager

Ref: How to get the mobile number of current sim card in real device?

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.)

OR

Ref: TelephonyManager give me null

> getLine1Number(); returns whatever is stored on the SIM card's MSISDN,
> which isn't filled by some operators.

In your case i am pretty much sure The MSISDN is unfilled.

You should keep one thing in mind.

getLine1Number() sometimes return with null value or with the phone number originally stored in SIM card.

How to get phone number or sim card information use android studio?

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();
}
}

Get mobile Number on GSM Mobile using android code

For GSM, the phone number is on the SIM card, and some carriers just don't put it on the card, and then the phone does not know what it is, but in this case you
should get an empry string rather than a null

if the carrier stores the number on your SIM, go to Settings -> About Phone -> Status -> My phone Number. If it displays unknown there, then your number is not stored on the SIM.

Sample Image

get IMEI use:

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

How to get the phone number and SIM card slot for the current device, on the callback of onCallAdded?

Use call.getDetails().getAccountHandle() to get PhoneAccountHandle.

Then use TelecomManager.getPhoneAccount() to get PhoneAccount instance.


Permission Manifest.permission.READ_PHONE_NUMBERS is needed for applications targeting API level 31+.


Disclaimer: I am no Android expert, so please do validate my thoughts.


EDIT: So solution for both before API 30 and from API 30 could be as such:

@RequiresApi(Build.VERSION_CODES.M)
fun handleCall(context: Context, call: Call) {
var foundAndSetSimDetails = false
val callDetailsAccountHandle = callDetails.accountHandle
val subscriptionManager = context
.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager
val telephonyManager =
context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
val telecomManager =
context.getSystemService(Context.TELECOM_SERVICE) as TelecomManager
val hasReadPhoneStatePermission =
ActivityCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == android.content.pm.PackageManager.PERMISSION_GRANTED
val phoneAccount = telecomManager.getPhoneAccount(callDetailsAccountHandle)
//TODO when targeting API 31, we might need to check for a new permission here, of READ_PHONE_NUMBERS
//find SIM by phone account
if (!foundAndSetSimDetails && phoneAccount != null && hasReadPhoneStatePermission) {
val callCapablePhoneAccounts = telecomManager.callCapablePhoneAccounts
run {
callCapablePhoneAccounts?.forEachIndexed { index, phoneAccountHandle ->
if (phoneAccountHandle != callDetailsAccountHandle)
return@forEachIndexed
if (!phoneAccount.hasCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION))
return@run
//found the sim card index
simName = phoneAccount.label?.toString().orEmpty()
simIndex = index + 1
foundAndSetSimDetails = true
return@run
}
}
}
//find SIM by subscription ID
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R && hasReadPhoneStatePermission) {
try {
val callSubscriptionId: Int =
telephonyManager.getSubscriptionId(callDetailsAccountHandle!!)
for (subscriptionInfo: SubscriptionInfo in subscriptionManager.activeSubscriptionInfoList) {
val activeSubscriptionId: Int = subscriptionInfo.subscriptionId
if (activeSubscriptionId == callSubscriptionId) {
setSimDetails(telephonyManager, subscriptionInfo)
foundAndSetSimDetails = true
break
}
}
} catch (e: Throwable) {
e.printStackTrace()
}
}
//find SIM by phone number
if (!foundAndSetSimDetails && hasReadPhoneStatePermission) {
try {
val simPhoneNumber: String? = phoneAccount?.address?.schemeSpecificPart
if (!simPhoneNumber.isNullOrBlank()) {
for (subscriptionInfo in subscriptionManager.activeSubscriptionInfoList) {
if (simPhoneNumber == subscriptionInfo.number) {
setSimDetails(telephonyManager, subscriptionInfo)
foundAndSetSimDetails = true
break
}
}
if (!foundAndSetSimDetails)
}
} catch (e: Throwable) {
e.printStackTrace()
}
}

private fun setSimDetails(telephonyManager: TelephonyManager, subscriptionInfo: SubscriptionInfo) {
var foundSimName: String? = null
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
val telephonyManagerForSubscriptionId =
telephonyManager.createForSubscriptionId(subscriptionInfo.subscriptionId)
foundSimName = telephonyManagerForSubscriptionId.simOperatorName
}
if (foundSimName.isNullOrBlank())
foundSimName = subscriptionInfo.carrierName?.toString()
simName = if (foundSimName.isNullOrBlank())
""
else foundSimName
simIndex = subscriptionInfo.simSlotIndex + 1
}

How to fetch current sim number

It is not guaranteed that getLine1Number() will always return SIM card's number on all devices. Because the availability of number depends on its storage in the SIM card.

If the number is not stored/available in SIM card then the method will return empty string.

In order to get user's phone number, you should send an sms from user's device to your server or device (same as Viber or other app does). And for a good practice make sure it is done with user's acknowledgement.



Related Topics



Leave a reply



Submit