I am developing an app that requires to get the IMSI. I use:
TelephonyManager mTelephonyMgr = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
String imsi = mTelephonyMgr.getSubscriberId();
It works for most phones, but a few handsets only return 6 digits instead of 15. Which is wrong.
Anyone knows an alternative way to retrieve the IMSI programatically? Other APIS? methods?
Regards
According to this post you can use
String imsi = android.os.SystemProperties.get(android.telephony.TelephonyProperties.PROPERTY_IMSI);
but SystemProperties is not directly accessible, so you will need to use one of the tricks in the answers for this question: Where is android.os.SystemProperties
You may also need SystemProperties source.
first: from Wiki
An IMSI is usually presented as a 15
digit long number, but can be shorter
shorter refers to an older imsi model that was 14 digits. it isn't relevant here
second: it not depends on handset but rather on network
it returns 6 digits because the android software on that particular handset is configured to return only the non-personal identifying part of the imsi - the first 6 digits which define the country and network operator
This code works well in my project.
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String imsi = telephonyManager.getSubscriberId();
and don't forget the permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Related
I need to make sure the SIM card can be used without entering pin in designated device(s), but it must be required in other devices. (Devices in use are rooted, application is installed as system app)
What I want is to identify a SIM card, and enter the pin programmatically, if one is already known/saved.
I am able to enter the known pin code, when one is required, but I have a problem with SIM identification.
I found out that I am not able to get SIM serial number or subscriber id before the correct PIN is entered.
The method I am using (with permissions and etc.):
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.getSimSerialNumber();
The thing is, this method returns null, if pin is required and not yet entered.
My question is if there is some kind of workaround for this.
Is it possible at all?
Is there another approach?
Ideas and approaches appreciated.
Thanks in advance.
TelephonyManager tm = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
}
String simID = tm.getSimSerialNumber();
String telNumber = tm.getLine1Number();
String IMEI = tm.getDeviceId();
Permission:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Your issue:
The phone number returned is whatever is stored on the SIM card's MSISDN, which is not filled by some operators. Try Airtel, Vodafone if in India.
I want get a phone number, but in device I get "" (empty). Any suggest?
This is my code:
TelephonyManager tMgr = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
mPhoneNumber is empty.
Manifest has:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
Some models return the phone number, some don't. I had the need for getting the phone number and ended up using Twitter's Fabric Digits. Easy to setup and free to use.
That API call is probing your sim card. Some telecom companies don't require the population of the phone number field in order to operate. So if your sim card doesn't have that information your string will be blank each time.
I am using a dual Sim phone And sometimes I get different IMEI numbers using the following code:
TelephonyManager manager = (TelephonyManager)
MyApplication.getInstance().getSystemService(Context.TELEPHONY_SERVICE);
String imeiNumber = manager.getDeviceId();
How to resolve this.
As per this question, device manufacturers modify Android to support dual SIMs. However, you can use reflection to get the details of both SIMs.
The following code does not give me the phone number of the device ,how can i get the phone number
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = tm.getLine1Number();
Toast.makeText(getApplicationContext(),"Telephone number: "+ number,
Toast.LENGTH_SHORT).show();
ALso i have tried the following..but the number doest show up
Programmatically obtain the phone number of the Android phone
How to get the mobile number of current sim card in real device?
The only method available from Android API as all the comments suggest is getLine1Number(). However I have never been able to obtain it since your operator must provide you with a SIM that supports letting phone read internal phone number. That seems not very common, so I am afraid that you could be left without means of knowing it.
That's why some programs that use your phone number to identify you (i.e. Whatsapp) do ask user about his/her phone number, because there is no sure way to getting it programatically.
the code provided:
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
should work fine, however, it will not if you forgot the permission "READ_PHONE_STATE".
Hope this works now :)
Try this:
private String getMyPhoneNumber(){
TelephonyManager mTelephonyMgr;
mTelephonyMgr = (TelephonyManager)
getSystemService(Context.TELEPHONY_SERVICE);
return mTelephonyMgr.getLine1Number();
}
Hope it help you
[In case if anyone still stumbles here looking for this still existing age-old problem]
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimised with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
I use the TelephonyManager to do this and you can access TelephonyManager s methods as given below
TelephonyManager telephonyManager = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
String number = telephonyManager.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 available.
I have also given application permission to make this query by adding the following to your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
The number is always blank or null...
While I have also crosschecked calling and i'm able to call from that number(i.e inserted sim no)
I have gone through numerous documentations but nothing help me out to get the phone number from sim so as to be used in my app..
With Regards,
Arpit Garg
AFAIK, TelephonyManager.getLine1Number() is not reliable due to various constraints from operators.
There are some java reflection based hacks but varies from device to device thus making those hacks sort of useless [at least in terms of supported models]
But there is a legitimate lawful logic to find the number, if you really need so. Query all the SMS by sms provider and get the "To" number.
Extra benefits of this trick: 1. you can get all the line numbers if there is multi sim in the device.
Cons: 1. you will need SMS_READ permission [sorry for that]
2. You will get all the sim numbers ever used in the device. this problem can be minimized with some constraint logic e.g. time frame (sms received or sent only today) etc. It would be interesting to hear from others about how to improve this case.
you should try this
TelephonyManager phoneManager = (TelephonyManager)
getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
String phoneNumber = phoneManager.getLine1Number();