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:
ListNote: 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 Changecellinfo = tm.getNeighboringCellInfo(); for(NeighboringCellInfo info: cellinfo){ cellid = info.getCid(); rssi = info.getRssi(); }
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
|
| 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
|
| 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 |
{ 3 comments… read them below or add one }
The “NeighboringCellInfo” array never gets populated until just before a handoff usually. I’ve noticed it fills at the point the mobile is considering reselection to a neighbor. In short it’s not a running list of potential neighbors.
But of my Tab [HTC Flyer], IMEI return as “NULL”
where is the problem???
Can i get city name of user from Telephony Manager?