Using Android TelephonyManager

Download source code of this article.

Android TelephonyManager provides information about the android telephony system. To use the TelephonyManager first get the instance of the Telephony Service by calling Context.getSystemService(TELEPHONY_SERVICE). This telephony service can be used to retrieve Call State, Cell Location, Operator Name, etc… as well as to listen to the various telephony events. Following are some of the important information we can get from TelephonyManager:

Cell Location

The getCellLocation method is used to get the Cell Location of the device. This method returns an instance of GsmCellLocation class. The getCid() and getLac() methods of this class can used to retrieve the Cell ID and LAC of the device. This method requires the permission ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION. Following code snippet shows how to retrieve the Cell ID and LAC.

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
GsmCellLocation loc = (GsmCellLocation) tm.getCellLocation();

int cellid = loc.getCid();
int lac = loc.getLac();

IMEI/MEID

The getDeviceId() method is used to get the IMEI/MEID of the device. If the device is a GSM device then IMEI will be returned and if the device is a CDMA device then MEID will be returned. This device id can be used to uniquely identify the device. This method requires the permission READ_PHONE_STATE. Following code snippet retrieve the device id.

String deviceid = tm.getDeviceId();

Device Phone Number

The getLine1Number() method returns the device phone number (MSISDN). This method requires the permission READ_PHONE_STATE. Following code retrieve the device phone number:

String phonenumber = tm.getLine1Number();

Note: This function name not work prior to Android Version 2.0.

Network Name

The getNetworkOperatorName() method returns the registered network operator’s name, getNetworkOperator() method returns the MCC + MNC of the registered network operator and getNetworkCountryIso() returns the registered network operator’s country code. This information may not be available on CDMA devices. Following code snippets retrieves these details:

String operatorname = tm.getNetworkOperatorName();
String operatorcode = tm.getNetworkOperator();
String operatoriso = tm.getNetworkCountryIso();

SIM Card Infomarion

The getSimCountryIso() returns the SIM operator’s country code, getSimOperator() returns the SIM operator’s MNC + MCC number, getSimOperatorName() returns the SIM operator’s name and getSimSerialNumber() returns the SIM Serial Number. This method requires the permission READ_PHONE_STATE. Following code snippets reads the SIM Card information:

String simcountrycode = tm.getSimCountryIso();
String simoperator = tm.getSimOperatorName();
String simserialno = tm.getSimSerialNumber();

Network Type

The getNetworkType() returns the type of the network available in the device. This method returns one of the following values:

TelephonyManager.NETWORK_TYPE_UNKNOWN
TelephonyManager.NETWORK_TYPE_GPRS
TelephonyManager.NETWORK_TYPE_EDGE
TelephonyManager.NETWORK_TYPE_UMTS

Phone Type

The getPhoneType() returns the device type. This method returns one of the following values:

TelephonyManager.PHONE_TYPE_NONE
TelephonyManager.PHONE_TYPE_GSM
TelephonyManager.PHONE_TYPE_CDMA

Subscriber ID

getSubscriberId() return the subscriber id of the device. This is IMSI if the device is a GSM device. If unavailable the function returns null. This function requires the permission READ_PHONE_STATE.

Neighboring Cell Information

The getNeighboringCellInfo() function returns a list of NeighboringCellInfo class which represents the neighboring cell information if available otherwise the function returns null. This function returns the permission ACCESS_COARSE_UPDATES. Following code snippet returns the this information:

List cellinfo = tm.getNeighboringCellInfo();

for(NeighboringCellInfo info: cellinfo){
    cellid = info.getCid();
    rssi = info.getRssi();
}

Note: I never got this working. I search a lot but I didn’t get any useful information why it is not returning value. Also when I ran this on a Android Version 1.6 Developer Phone I got a Unknown permission android.permission.ACCESS_COARSE_UPDATES. Please let me know if you get this working. Listening to Phone State Change
The listen() method is used to register a phone state listener. It accepts a PhoneStateListener instance and an int value specifying what are the events to listen. Following are the events we can listen:

PhoneStateListener.LISTEN_SIGNAL_STRENGTH
PhoneStateListener.LISTEN_DATA_ACTIVITY
PhoneStateListener.LISTEN_CELL_LOCATION
PhoneStateListener.LISTEN_CALL_STATE
PhoneStateListener.LISTEN_CALL_FORWARDING_INDICATOR
PhoneStateListener.LISTEN_DATA_CONNECTION_STATE
PhoneStateListener.LISTEN_MESSAGE_WAITING_INDICATOR
PhoneStateListener.LISTEN_SERVICE_STATE

PhoneStateListener class has following method:

void onCallForwardingIndicatorChanged(boolean cfi)
void onCallStateChanged(int state, String incomingNumber)
void onCellLocationChanged(CellLocation location)
void onDataActivity(int direction)
void onDataConnectionStateChanged(int state)
void onDataConnectionStateChanged(int state, int networkType)
void onMessageWaitingIndicatorChanged(boolean mwi)
void onServiceStateChanged(ServiceState serviceState)
void onSignalStrengthChanged(int asu)
void onSignalStrengthsChanged(SignalStrength signalStrength)

Listening to phone state requires a separate article to describe, so this will be explained in my next post.

Following table describes important functions and its permission:

Function Description
getCellLocation() Returns the the Cell Location of the device
ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION
getDeviceId() Returns the IMEI/MEID of the device. If the device is a GSM device
then IMEI will be returned and if the device is a CDMA device then MEID
will be returned
READ_PHONE_STATE
getLine1Number() Returns the device phone number (MSISDN)
READ_PHONE_STATE
getNetworkOperatorName() Returns the registered network operator’s name
getNetworkOperator() Returns the MCC + MNC of the registered network operator
getNetworkCountryIso() Returns the registered network operator’s country code
getSimCountryIso() Returns the SIM operator’s country code
READ_PHONE_STATE
getSimOperator() Returns the SIM operator’s MNC + MCC number
READ_PHONE_STATE
getSimOperatorName() Returns the SIM operator’s name
READ_PHONE_STATE
getSimSerialNumber() Returns the SIM Serial Number
READ_PHONE_STATE
getNetworkType() Returns the type of the network available in the device. This will be
one of the following values:

TelephonyManager.NETWORK_TYPE_UNKNOWN

TelephonyManager.NETWORK_TYPE_GPRS

TelephonyManager.NETWORK_TYPE_EDGE

TelephonyManager.NETWORK_TYPE_UMTS
READ_PHONE_STATE

getPhoneType() Returns the device type. This will be one of the following values:

TelephonyManager.PHONE_TYPE_NONE

TelephonyManager.PHONE_TYPE_GSM

TelephonyManager.PHONE_TYPE_CDMA
READ_PHONE_STATE

getSubscriberId() Return the subscriber id (IMSI) of the device
READ_PHONE_STATE
getNeighboringCellInfo() Returns a list of NeighboringCellInfo class which represents the
neighboring cell information if available otherwise the function
returns null
ACCESS_COARSE_UPDATES

8 Responses to “Using Android TelephonyManager”

  1. Thanks for the great article,

    is it possible to get informations about the TMSI (Temporary Mobile Subscriber Identity) and other GSM specific values? (e.g. is Frequency Hopping activated, used Time Slot)

    Have you fixed the problem with the neighbour cell informations?

    Thanks

    Reply
    • Truwer, so far I didn’t find methods to retrieve these values from Android device, will keep you updated here about the progress.

      Regarding the Cell Information, I am getting correct information from my Nexus One (2.3.4), what issue you are facing to get the Cell Information?

      Thanks

      Reply
  2. Mohamed Sobhy Sayed

    My device always returns null when I ask for mobile phone no.?!!
    I read something about that the SIM in some cases doesn’t hold the number on it, it is supported through the provider, is that the problem or am I doing something wrong?
    I added the permission in the manifest and wrote
    “TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    showMsg(“Phone No: ” + telephonyManager.getLine1Number());”

    Thanks in advance.

    Reply
  3. Cougar

    I guess that the reason why getNeighboringCellInfo returns null (at least from service) is here in PhoneInterfaceManager.java code:


    if (checkIfCallerIsSelfOrForegoundUser()) {
    if (DBG_LOC) log("getNeighboringCellInfo: is active user");

    ArrayList cells = null;

    try {
    cells = (ArrayList) sendRequest(
    CMD_HANDLE_NEIGHBORING_CELL, null);
    } catch (RuntimeException e) {
    Log.e(LOG_TAG, "getNeighboringCellInfo " + e);
    }
    return cells;
    } else {
    if (DBG_LOC) log("getNeighboringCellInfo: suppress non-active user");
    return null;
    }

    I really do not know what is the reason behind this

    Reply
  4. Question:
    In case of dual SIM card supported mobile phone and when two SIM card are inserted, how to get two SIM card information?
    TelephonyManager gets the instance of the Telephony Service by callingContext.getSystemService(TELEPHONY_SERVICE) like you said, but there are not any parameter or method can chose specified SIM card.

    Reply
  5. Krishnaraj,
    Did you have any progress to retrieve the TMSI? I am looking for alternatives to get that information from my smartphone (Android 4.2).

    Thanks,

    Pedro

    Reply
  6. nitisha

    krishnaraj,
    i am trying to do little bit same. in my case i got neighboring cell info.but give me same provider cell tower info. did you know how to get neighboring cell info for all providers.
    please let me know if you can suggest something regarding this.
    thanks

    Reply

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>