Search code examples
androidtelephonytelephonymanager

Why can't I get the mobile number of a SIM card from Denmark?


To get the mobile number of the user, this is what I do:

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_MANAGER);
String mobileNumber = tm.getLine1Number();

This method seems to work 100% of the time in the Philippines, where SIM cards are mostly prepaid. If you need to change your number, you just buy a new one. In Denmark, however, I always get nothing. Why is that? And is there a way for me to get the mobile number of the user besides this one?


Solution

  • Your phone number is stored in SIM card's memory (here I'm not talking about contacts memory). It depends on the SIM card manufacturer if they add the number into your SIM card or not. So, there might be a possibility that your SIM card doesn't contain the number.

    To solve this, you need to manually define your phone number in SIM. Take any old SonyEricsson phone or iPhone, go to SIM settings (depending on the phone model) and define your number for the SIM card. Once its done, your code will be able to read it from SIM.

    In may case, I am using a Tre-Sverige SIM card which also didn't have the phone number stored on SIM, but after following the procedure above, now I'm able to read the number.