How can I get the phone number without getLine1Number() method - android

I'm having troubles getting the phone number in certain phones with EURO operators, I try with TelephonyManager using getLine1Number() for get the SIM number, that work fine but in order to get a GSM phone number the method return a empty string. So, the question is, how can I get that info without getLine1Number() method?
Any way is valid, just I need take that information and send SMS to user or register using a phone number as username.

In some countries, SIM does not store phone numbers. If you want an unique user number, you can use subscriber ID:
TelephonyManager telMan = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = "00";
try {
phoneNumber =telMan.getLine1Number();
} catch(NullPointerException ex) {
ex.printStackTrace();
}
if(phoneNumber.equals("")){
phoneNumber = telMan.getSubscriberId();
}

Related

SubscriptionInfo.getMnc() drops leading zeros, cannot link MCC+MNC to SIM operator

I'm updating some code to use the Multi SIM support that was added to Android in 5.1. We examine the SIM operator (or the MCC+MNC combo) a lot in our app, so the code I'm writing involves migrating from reliance on TelephonyManager.getSimOperator() to getting SubscriptionInfos from the SubscriptionManager, each of which gives access to the MCC and MNC for a SIM.
//old way – can only access one SIM
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String simOperator = telephonyManager.getSimOperator();
//new way - gives access to all SIMs
SubscriptionManager subscriptionManager = (SubscriptionManager) context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE);
List<SubscriptionInfo> subInfoList = subscriptionManager.getActiveSubscriptionInfoList();
for(SubscriptionInfo info : subInfoList) {
int mcc = info.getMcc();
int mnc = info.getMnc();
//do some stuff...
}
I'm finding that because it's returning ints for MCC and MNC, you lose leading zeros that are in some of the MNCs. To see some examples, check out the table here. When you concat MCC+MNC you get the SIM operator, so it is important to keep the zeros that are often present. For example, NEXTEL in Mexico has a SIM operator value of "334090," but because the SubscriptionInfo returns ints for MCC and MNC, you get the values 334 and 90, respectively.
Given this issue, how can you reliably link a SubscriptionInfo's MCC+MNC pair to a particular SIM operator in a list of known SIM operators? Is this an oversight in the design of this API? Is there any other way to get the SIM operator for every SIM card that's in the multi-SIM phone?
MCC are always 3 digits, the rest is MNC, which as you correctly said, may have 2 or 3 digits.
If you parse the int to String, you can just get sub string of it and process the info as required.
Also it's a hidden method, but you can access it by reflection, to get the info you want in String format, this exists at least from Android 6 and Android 7
public String getSimOperator(int subId)
But I think SubscriptionManager way is safer since it's open API and for the moment you don't need to bother with Android versions handling it differently.
The best way to achieve this is to read IMSI from the SIM card. If you are reading the MNC+MNC directly and your user is on roaming or connected to another network then the values returned won't be reliable.
E.g. If your user is NEXTEL customer but is travelling or due to network connection issues connects to another network, lets say Telcel, then the value returned from MCC+MNC will be from Telcel.
In order to reliably match the operator of current SIM to predefined list, read IMSI value from SIM card using getSubscriptionId() from Telephony manager.
IMSI starts with MCC+MNC of the operator. You can easily check if MCC+MNC matches as follows
final String mccMncToCompare = "334090"; //You can also use Integer instead of string. You just have to perform type conversion
final TelephonyManager tm = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
final String imsi= tm.getSubscriberId();
final boolean mccMncMatched = imsi.startsWith(mccMncToCompare);

Get my telephone number programatically on android

I am trying to get my telephone number programatically on Android. I have readen that always is not possible to get the number but perhaps somebody knows some solution to do this. Thank you. Here is my code:
public class ReadSIM extends Activity
{
public void onCreate(Bundle savedInstance)
{
super.onCreate(savedInstance);
setContentView(R.layout.main);
TelephonyManager telemamanger = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
TextView mitexto =(TextView) findViewById(R.id.textView1);
mitexto.setText(telemamanger.getLine1Number().toString());
}
}
And here is the manifest permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Ya it is not always possible to get Contact Number,because it mainly depends on sim card is it stores your number on your cell phone.
As per i know there is no other way to get Active Sim card number except this trick.
In place of that you can fetch IMSI number of that sim card.It is also unique.
And it is also available for developers.
Your code is correct. But not all the mobile phones returns you the telephone number. But in the most of the cases telemamanger.getLine1Number().toString() returns you an empty string.

How to know SMS number and phone number is the same?

I'm working on a project that is similar to Message in android.
There are 2 types of number : sms number and phone number.
For example : I always receive sms with number is: +84973612399. But her phone number is 0973612399. How can I know that 2 numbers only belongs to a person?
Thanks.
Use PhoneNumberUtils.compare to compare both numbers.
Example
//Compare phone numbers a and b, return true if they're identical enough for caller ID purposes.
if (PhoneNumberUtils.compare("+84973612399", "0973612399")) {
Log.d(TAG, "Both are identicaly same");
} else {
Log.d(TAG, "Doesn't match");
}
Result is
Both are identicaly same

How to find out carrier's name when using MVNO? (i want the one written in notification Bar)

I am just trying to get my carrier name.
The issue while trying to use:
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE));
String carrierName = manager.getNetworkOperatorName();
That is working great unless you are a MVNO (definition here: http://en.wikipedia.org/wiki/Mobile_virtual_network_operator)
For instance, in Belgium, we have the "Base" carrier that rent the network to some MVNO, like Toledo, Allo RTL, Mobile Vikings, etc...
In my notification bar, when using such virtual operators, I get their real names, but the code above just give me "Base" as String, and not the one I want.
I hope you understand what I mean.
I don't have a MVNO SIM card but the getSimOperatorName() method of the TelephonyManager might be the method you are looking for.
TelephonyManager manager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String carrierName = manager.geSimOperatorName();
I have a MVNE SIM card in Germany and the method returns an empty String, but the getNetworkOperatorName() method returns the right Operator for my SIM card.

Get country based on GSM/UMTS network

I need to detect the MCC of the country in which the GSM/UMTS wireless modem is currently now.
Based on GSM networks
You need to use getSimCountryIso() and getNetworkCountryIso() from the TelephonyManager
Returns the ISO country code equivalent for the SIM provider's country code.
Based on WIFI you use a Ip to Country database
You have also the option to use the Geocoder class based on the location
First get the MCC/MNC:
TelephonyManager tel = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String networkOperator = tel.getNetworkOperator();
if (networkOperator != null) {
int mcc = Integer.parseInt(networkOperator.substring(0, 3));
int mnc = Integer.parseInt(networkOperator.substring(3));
}
Then, based on that, you can get the number corresponding to the selected MCC. There are plenty of lists on the internet, for example this one on Wikipedia

Categories

Resources