I am trying to develop an application that needs the phone number of SIM Card every time a new SIM Card is inserted in the phone....Then i will use that phone number to get the user subscribed to an online server.I have read many forums and found something like this
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
mPhoneNumber = tMgr.getLine1Number();
but everyone said that this is not an efficient method.If this is not an efficient method then which is the efficient method and also I cannot use any other property like SIM id or Subscriber ID etc.I only need the PHONE NUMBER. Any help would be appreciated.
As far as I know, this method would not always return right SIM number; it depends on the SIM card itself.
If you really want to get the SIM number, you can send a SMS to a specific phone, so you will get it.
TelephonyManager is not the right Solution,Because in some cases the
number is not stored in the SIM, Due to my suggestion,You should use
Shared Preference to store user's Phone number first time the
application is open, and after that the number will used whenever you
need in application.
Thank you.
Related
I want to read which sim card allocated for default voice call in sim management settings programmatically .Is that possible? I have tried to open setting provider by intent but I cant able read the sim slots allocated for calls
I dont think its possible. But hey, if you want to know which sim was used for making call, you can get it from call logs database. There is a column for sim slots in the table.
This isn't a definite answer, I'm also looking for this function.
But I managed to find a solution which works partially. You can use the TelephonyManager to obtain the preferred SIM.
TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
String activeSIMNetwork = tm.getNetworkOperator();
The catch here, is that this preference is not sometimes valid. If you change from one sim to another and check, it will give you the correct preferred SIM set in the phone. But if you reboot the phone and check, it will not give you the correctly displayed preference.
I will post an update if I get it to work in some other way.
I tried getLine1number(), for some device it gets the number. but for some, it returns empty string("").
As far as I tested, when in the device's Settings > Status > My Phone number is not empty, I get the number otherwise I am not able to get the Phone number.
Is there any other way to get the Phone number other than the above method?
As per my knowledge their is no any way to find a telephone number other than .getLine1Number() method.
Provide you some options that you try:-
A) If number return null means, your phone number not register in your mobile.For that you can use the sim card serial number.It is unique number.
TelephonyManager telemamanger = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String getSimSerialNumber = telemamanger.getSimSerialNumber();
B) The method you are using is the only one part of the SDK to do this,
and only works on devices where the number is stored on the SIM card,
which only some carriers do. For all other carriers,
you will have to ask the user to enter the phone number manually,
as the number is simply not stored anywhere on the device from where you can retrieve it.
Hope it will helps you.
I am developing an application that I need to find user phone number.
I founded Telephony Manager but this class does not guarantee that the phone number always returns.so i want to know is there any solution(probably not a straight solution that can i give users phone number because the number does not coded on sim card) in android?
Only few service providers provide phone number via getLine1Number() method. Instead you can get a unique number for each sim using getSimSerialNumber() method.
Ask the user to enter phone number, you can store his simnumber using getSimSerialNumber();
Then authenticate depending on the sim number, this should solve your problem
You don't have much possibilities here you can use either :
TelephonyManager tMgr =(TelephonyManager)mAppContext.getSystemService(Context.TELEPHONY_SERVICE);
String mPhoneNumber = tMgr.getLine1Number();
Which as you said is not guaranteed to always return a number. Or you can ask user to enter the number manually. I think there's no other option.
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 want to get the mobile number of a sim which is inserted into the device. I know as pr the android SDK TelephoneManger is the class by which we can get it by getLine1Number() method. but it always returns null values.
Queries:
Can i get mobile number from
device?
Is it a sim dependent or in some case
also device dependent?
Any other ways to get the mobile number from device?
You are using correct and the only approach available to the common user (AFAIK). It is mainly SIM dependent (it is not obligatory to have ), and in some cases may be device dependent (getLine1Number() not correctly implemented).
To be more precise, IMSI uniquely identifies SIM. But MSISDN (the subscriber number) is not uniquely related to SIM, as it is possible to change MSISDN on the SIM. Network may use IMSI from SIM to find MSISDN in the HLR and establish the call.
So, MSISDN is not mandatory to be on the SIM.
You should look at this question and its comments.
Some SIM cards seems to cause getLine1Number() to return null... and no one has found a way to deal with this.
More information here
In that case you always save your phone no using SharedPreference by using the method getLine1Number() method. when you install your application then your phone number is saven and when when new sim card is used then send SMS to ur registered number. you have to always check after starting the application that your stored number is same with your new number.if same nothing will be happned ,if not same then ur app will send SMS.