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.
Related
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);
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();
}
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.
I want to get my network operator name in my app.
I am using fallowing methods in the TelephonyManager to get this:
TelephonyManager mTeleManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
mTeleManager.getSimOperatorName();
mTeleManager.getNetworkOperatorName();
mTeleManager.getSimOperator()
But I am getting empty values from the "getSimOperatorName()" and "getNetworkOperatorName()"..
I am getting numeric code from this "getSimOperator()" method, But I want know the alphabetic form of the operator name . Some thing like "T-Mobile" for Tmobile networks.
Pl. suggest me how to do this.
thanks.
Have you tried it on different phones/with different sim cards?
According to TelephonyManager.getLine1Number() returns Null
Hi All,
Here comes an explanation for this. It is not actually an issue, both information depends on some fields on the SIM card that are optional. So it might be empty if the SIM has no such optional fields or if the operator has set it empty.
Regards,
He is talking about
TelephonyManager.getLine1Number()
TelephonyManager.getSimOperatorName()
Is it possible to find Base Station Identifying Code in android. To clarify, Let 'A' calls to 'B'. Before 'B' receive the phone call, his installed app will find
'A's BSIC and match it with a given database of BSIC( this BSIC database may be located on phone locally or
in a web or from phone network provider's database ) then will find 'A' location( Say +88017... is calling from "Dinajpur of Bangladesh" ).Is it possible ? if so how ? please help me...
Rereading the question, you want the other person's tower, not your own. No, i don't think that info is available to you. At most, you can guess by the country and region codes.
Not relevant anymore:
I believe you can achieve what you want getting the cell id in use or all the neighboring cells
TelephonyManager mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
GsmCellLocation loc = (GsmCellLocation) mTelephonyManager.getCellLocation();
Log.d ("CID", Integet.toString(loc.getCid()));
Log.d ("LAC", Integet.toString(loc.getLac()));
// or
List<NeighboringCellInfo> list = mTelephonyManager.getNeighboringCellInfo ();
for (NeighboringCellInfo cell : list) {
Log.d ("CID", Integet.toString(cell.getCid()));
Log.d ("LAC", Integet.toString(cell.getLac()));
}
You can refer then to cell location through several open databases (e.g., http://www.location-api.com/ or http://opencellid.org/ )
If this is not enought, I suggest you to check this thread: http://groups.google.com/group/android-platform/browse_thread/thread/b55c8d3275ed7042/78d9c30c49e94a3a , specially this link: http://www.google.com/url?sa=D&q=http://www.3gpp.org/ftp/Specs/archive/27_series/27.007/27007-860.zip