Click here to Skip to main content
11,925,851 members (60,122 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


39 bookmarked

Android Phone Status Sample

, 22 Mar 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
A Simple android application to display the phone details, battery status and data connectivity status of an Android Smartphone


This article shall make use of API’s inside android.telephony and android.os.batterymanager packages present in the Android SDK to create a simple yet smart application to display the phone details, battery status and data connectivity status of an Android Smartphone.


The Android framework gives you everything you need to build best-in-class app experiences. The core component of this is the Android Software Development Kit (Android SDK) that provides all necessary tools to build, test and deploy an application. In this article we mainly focus on the following packages / classes to display some of the very common information related to a cellular phone.   

 Service Provider Related 

  • Service state 
  • Cell Location
  • Call State
  • Connection State 
  • Signal Strength
  • Data Exchange.

 Phone Specific   

  • Device ID
  • Phone number
  • SW Version 
  • Operator Name
  • SIM Country Code 
  • SIM Operator
  • SIM Serial No 
  • Subscriber ID 
  • Network type
  • Phone type  
  • Battery Related Information 

In order to retrieve the above mentioned information from the Android system, we have to include the permissions in our android application in the AndroidManifest.xml file. The below mentioned list presents the user with appropriate permissions that are required to be included in AndroidManifest.xml file for a certain information request.

Permissions in Android manifest 

No  InformationPermission
1  Cell location 



2Call State
3Data Connection State
Signal Strength
5Data Direction States
6Service State

SIM Operator 

Phone Type


Designing the Android application layout 

The next step is to design a layout file which shall display the information that we are interested in.  For the sake of clarity i have included a tabbed layout in this sample. The first tab shall display the telephony related information while the second tab shall show the current battery status of the phone.

The main.xml  

This layout shall host a tab widget. Depending on the selection shall display the Phone Status or Battery Status information  

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android=""
            android:layout_height="wrap_content" >
            android:layout_height="match_parent" >


This layout shall display the telephony related information such as Call State,Cell location, Connection state, Signal level, Data activity, Phone number , IMEI code , Device SW version and so on. 

<ScrollView xmlns:android=""
                <!--Service State-->
                        <TextView android:text="@string/tvServiceState" style="@style/labelStyleRight"/>
                        <TextView android:id="@+id/serviceState_info" style="@style/textStyle"/>
                <!--cell location -->
                        <TextView android:text="@string/tvCellLocation" style="@style/labelStyleRight"/>
                        <TextView android:id="@+id/cellLocation_info" style="@style/textStyle"/>
                <!--Call State-->
                        <TextView android:text="@string/tvCallState" style="@style/labelStyleRight"/>
                        <TextView android:id="@+id/callState_info" style="@style/textStyle"/>
                <!--Data Connection State-->
                        <TextView android:text="@string/tvConnState" style="@style/labelStyleRight"/>
                        <TextView android:id="@+id/connectionState_info" style="@style/textStyle"/>
                <!--Signal level -->
                        <TextView android:text="@string/tvSignalLevel" style="@style/labelStyleRight"/>
                                <ProgressBar android:id="@+id/signalLevel" style="@style/progressStyle"/>
                                <TextView android:id="@+id/signalLevelInfo" style="@style/textSmallStyle"/>
                <!--Data Activity-->
                        <TextView android:text="@string/strData" style="@style/labelStyleRight"/>
                        <ImageView android:id="@+id/dataDirection" style="@style/imageStyle"/>
                <TextView android:id="@+id/device_info" style="@style/labelStyleLeft"/>

Once the data has been assigned inside the PhoneStatusActivity (described later). The layout file would produce a screen as below


The battery.xml  

The layout shall serve as a placeholder to display the phone battery specific information.  The layout file consists of a simple textview. The data source for this text view shall be set inside (described later) 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""
    android:orientation="vertical" >
    <TextView android:id="@+id/batterylevel" style="@style/textStyleCenter"/>

The layout file shall output to something similar as shown below 

Android Layout Styles 

Styles in Android is analogous to cascading stylesheets (CSS) in web
design i.e they allow you to separate the design from the
content. For a consistent look and feel in the Battery Status and Phone Status layout we will include a styles.xml file inside the res/values folder which will specify properties such as height, padding, font color, font size,
background color,text alignment and much more.

To include a style.xml file inside Android project, let us create a new XML file under res/values folder and name it as styles.xml

<resources xmlns:android="">
    <style name="AppTheme" parent="android:Theme.Light" />
    <color name="blue">#0000FF</color>
    <color name="white">#FFFFFF</color>
    <color name="red">#FF0000</color>
    <color name="yellow">#FFF200</color>
    <color name="green">#00FF00</color>
    <color name="black">#000000</color>
    <style name="labelStyleRight">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:textSize">15dip</item>
                <item name="android:textStyle">bold</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center_vertical|right</item>
    <style name="labelStyleLeft">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:textSize">15dip</item>
                <item name="android:textStyle">bold</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center_vertical|left</item>
    <style name="textStyle">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:textSize">15dip</item>
                <item name="android:textStyle">bold</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center_vertical|left</item>
    <style name="textStyleCenter">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:textSize">15dip</item>
                <item name="android:textStyle">bold</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center</item>
    <style name="textSmallStyle">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">fill_parent</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:textSize">10dip</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center_vertical|left</item>
    <style name="progressStyle">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:indeterminateOnly">false</item>
                <item name="android:minHeight">20dip</item>
                <item name="android:maxHeight">20dip</item>
                <item name="android:progress">15</item>
                <item name="android:max">100</item>
                <item name="android:gravity">center_vertical|left</item>
                <item name="android:progressDrawable">@android:drawable/progress_horizontal</item>
                <item name="android:indeterminateDrawable">@android:drawable/progress_indeterminate_horizontal</item>
    <style name="imageStyle">
                <item name="android:layout_width">fill_parent</item>
                <item name="android:layout_height">wrap_content</item>
                <item name="android:layout_weight">0.5</item>
                <item name="android:src">@drawable/nodata</item>
                <item name="android:scaleType">fitStart</item>
                <item name="android:layout_margin">10dip</item>
                <item name="android:gravity">center_vertical|left</item>

Using the code    

1.     Permissions in the AndroidManifest.xml file   

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_UPDATES"/>

2.     The android.telephony.TelephonyManager  

Any information related to the telephony services on the device has to be accessed via TelephonyManager class. This class cannot be instantiated directly instead we retrieve a reference to an instance through 

TelephonyManager tm = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);  

2.1     The PhoneStateListener object     

The PhoneStateListener class is responsible for monitoring changes in specific telephony states

on the smartphone device. We shall override the methods for the state that we wish to receive updates for in the PhoneStateListener object and pass the PhoneStateListener instance, along with bitwise-or of the LISTEN_ flags to TelephonyManager.listen(). The application is now ready for updating any view based on the Telephony events  

int events = 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 |

tm.listen(phoneListener, events);

2.1.1     Listening to Phone States  

Upon specifying the updates we wish to receive from TelephonyManager. The PhoneStateListener shall handle the callback methods for each change in the phone state. We shall record all such state changes in the corresponding handler methods provided by the PhoneStateListener class and update the status on the view. 

NOTE: For demonstration purpose I've discussed only a handful of PhoneStateListener callback methods here in this article. Also the callback implementation methods are oversimplified for the sake of understanding. The methods do no more that updating a TextView or change of thumbnail image in some cases.          onCallStateChanged 

Callback invoked when device call state changes. In this method we shall record whether the phone is OFFHOOK, RINGING  or in the IDLE State based on the State.   

TelephonyManager.CALL_STATE_IDLEDevice call state : No activity
TelephonyManager.CALL_STATE_RINGING Device call state : A new call arrived and is

ringing or waiting  
TelephonyManager.CALL_STATE_OFFHOOK At least one call exists

that is dialing, active, or on hold, and no calls are ringing

or waiting 

 * Call State Changed
 * */
public void onCallStateChanged(int state, String incomingNumber) {
    String phoneState = "UNKNOWN";
    case TelephonyManager.CALL_STATE_IDLE :
        phoneState = "IDLE";
    case TelephonyManager.CALL_STATE_RINGING :
        phoneState = "Ringing (" + incomingNumber + ") ";
    case TelephonyManager.CALL_STATE_OFFHOOK :
        phoneState = "Offhook";
    //Sets the CallState on a textview
    setTextViewText(info_ids[INFO_CALL_STATE_INDEX], phoneState);
    super.onCallStateChanged(state, incomingNumber);
}          onDataConnectionStateChanged

This callback method is invoked whenever there is a change in the data connection state. It can return any of the following 4 possibilities  

TelephonyManager.DATA_CONNECTEDIndicates IP traffic should be available 
TelephonyManager.DATA_CONNECTINGCurrently setting up a data connection 
TelephonyManager.DATA_DISCONNECTED Indicates IP Traffic is not available
TelephonyManager.DATA_SUSPENDED The connection is up, but IP

traffic is temporarily unavailable. For example, in a 2G network,

data activity may be suspended when a voice call arrives 

 * Cellphone data connection status
 * */
public void onDataConnectionStateChanged(int state) {

    String phoneState = "UNKNOWN";
    case TelephonyManager.DATA_CONNECTED :
        phoneState = "Connected";
    case TelephonyManager.DATA_CONNECTING :
        phoneState = "Connecting..";
    case TelephonyManager.DATA_DISCONNECTED :
        phoneState = "Disconnected";
    case TelephonyManager.DATA_SUSPENDED :
        phoneState = "Suspended";
    //Sets the data connection status on a textview
    setTextViewText(info_ids[INFO_CONNECTION_STATE_INDEX], phoneState);
}          onDataActivity  

This Callback is invoked when data activity state changes.  The possible data activity states are as listed below

  • TelephonyManager.DATA_ACTIVITY_NONE  -  No IP Traffic.
  • TelephonyManager.DATA_ACTIVITY_IN        -  Receiving IP Traffic
  • TelephonyManager.DATA_ACTIVITY_OUT  -  Currently sending IP Traffic 
  •  TelephonyManager.DATA_ACTIVITY_INOUT  -  Currently sending and receiving IP Traffic 
  • TelephonyManager.DATA_ACTIVITY_DORMANT  -  Data connection is active, but physical link is down
 * Data activity handler
 * */
public void onDataActivity(int direction) {

    String strDirection = "NONE";
    case TelephonyManager.DATA_ACTIVITY_IN :
        strDirection = "IN";
    case TelephonyManager.DATA_ACTIVITY_INOUT:
        strDirection = "IN-OUT";
    case TelephonyManager.DATA_ACTIVITY_DORMANT:
        strDirection = "Dormant";
    case TelephonyManager.DATA_ACTIVITY_NONE:
    case TelephonyManager.DATA_ACTIVITY_OUT:
    //Updates the status Data Activity on a ImageView

The setDataDirection is a user defined method updates a corresponding thumbnail image on a ImageView based on the status of DataActivity .


private void setDataDirection(int id, int direction){
      int resid = getDataDirectionRes(direction);

2.1.2     End Listening to Phone States     

For the application to stop listening to the updates from the TelephonyManager class. We now shall pass the PhoneStateListener instance along with LISTEN_NONE flag to TelephonyManager.listen() method

tm.listen(phoneListener, PhoneStateListener.LISTEN_NONE);

2.2     Querying the Basic Phone Information  

The TelephonyManager readily exposes methods to access basic information related to phone such as subscriber information, IMEI Code, Phone no,Network type etc. The code related to this Phone status information is included in the

//Get the IMEI code
String deviceid = tm.getDeviceId();
//Get  the phone number string for line 1,For ex: the MSISDN for a GSM phone
String phonenumber = tm.getLine1Number();
//Get  the software version number for the device, For ex: the IMEI/SV for GSM phones
String softwareversion = tm.getDeviceSoftwareVersion();
//Get  the alphabetic name of current registered operator.
String operatorname = tm.getNetworkOperatorName();
//Get  the ISO country code equivalent for the SIM provider's country code.
String simcountrycode = tm.getSimCountryIso();
//Get  the Service Provider Name (SPN).
String simoperator = tm.getSimOperatorName();
//Get  the serial number of the SIM, if applicable. Return null if it is unavailable.
String simserialno = tm.getSimSerialNumber();
//Get  the unique subscriber ID, for example, the IMSI for a GSM phone
String subscriberid = tm.getSubscriberId();
//Get the type indicating the radio technology (network type)
//currently in use on the device for data transmission.EDGE,GPRS,UMTS  etc
String networktype = getNetworkTypeString(tm.getNetworkType());
//This indicates the type of radio used to transmit voice calls
//GSM,CDMA etc
String phonetype = getPhoneTypeString(tm.getPhoneType());

The user defined utility methods returns the equivalent string  

private String getNetworkTypeString(int type){
       String typeString = "Unknown";
               case TelephonyManager.NETWORK_TYPE_EDGE:
                   typeString = "EDGE"; break;
               case TelephonyManager.NETWORK_TYPE_GPRS:
                   typeString = "GPRS"; break;
               case TelephonyManager.NETWORK_TYPE_UMTS:
                   typeString = "UMTS"; break;
                   typeString = "UNKNOWN"; break;
       return typeString;

   private String getPhoneTypeString(int type){
           String typeString = "Unknown";
                   case TelephonyManager.PHONE_TYPE_GSM:   typeString = "GSM"; break;
                   case TelephonyManager.PHONE_TYPE_NONE:  typeString = "UNKNOWN"; break;
                       typeString = "UNKNOWN"; break;
           return typeString;

In this sample, Once we collect all the required information we shall display the same on a deviceInfo TextView

deviceinfo += ("Device ID: " + deviceid + "\n");
deviceinfo += ("Phone Number: " + phonenumber + "\n");
deviceinfo += ("Software Version: " + softwareversion + "\n");
deviceinfo += ("Operator Name: " + operatorname + "\n");
deviceinfo += ("SIM Country Code: " + simcountrycode + "\n");
deviceinfo += ("SIM Operator: " + simoperator + "\n");
deviceinfo += ("SIM Serial No.: " + simserialno + "\n");
deviceinfo += ("Subscriber ID: " + subscriberid + "\n");
deviceinfo += ("Network Type: " + networktype + "\n");
deviceinfo += ("Phone Type: " + phonetype + "\n");

2.3     Phone Battery  

The android.os.BatteryManager class provides information related to
the status of the phone battery in the form of strings and constants.   


Also we can extract both the current charging status and, if the device is being charged, whether
it's charging via USB or AC charger using  

  • BatteryManager.BATTERY_PLUGGED_AC  - Battery is plugged to an AC source 
  • BatteryManager.BATTERY_PLUGGED_USB  - Battery is plugged to a USB source  

  • BatteryManager.BATTERY_STATUS_CHARGING - Battery is connected to a power supply and is charging 
  • BatteryManager.BATTERY_STATUS_DISCHARGING - Battery is discharging
  • BatteryManager.BATTERY_STATUS_FULL - Battery charge is complete 

3.    Activity class for Smartphone Status App

3.1     The StatusActivity class

This is the main activity that is called on the start of the application. This activity is responsible for creating a tab host for hosting PhoneStatusActivity and BatteryStatusActivity. By default PhoneStatusActivity shall be activated. 

public void onCreate(Bundle savedInstanceState) {
        TabHost host = getTabHost();
        //Phone Status Activity 
        TabSpec statusspec = host.newTabSpec("Phone");
        Intent phoneStatusIntent = new Intent(this, PhoneStatusActivity.class);
        //Battery Status Activity
        TabSpec batteryspec = host.newTabSpec("Battery");
        batteryspec.setIndicator("Battery", getResources().getDrawable(R.drawable.nbattery));
        Intent batteryIntent = new Intent(this, BatteryStatusActivity.class);
        // Adding all TabSpec to TabHost
        host.addTab(statusspec); //Default tab  

3.2     The  PhoneStatusActivity  class

The PhoneStatusActivity class is responsible for displaying the information received from TelephonyManager as described in the Section 2.1 and Section 2.2.  

3.3     The BatteryStatusActivity class

The BatteryStatusActivity class is responsible for displaying the status of battery. The BatteryManager
broadcasts all battery and charging details in a sticky Intent
that includes
the charging status. By hooking in to these intents we can continuosly
monitor the status of the phone battery. To achieve this, we register a
BroadcastReceiver to be run in the main activity thread
receiver will be called with any broadcast Intent that matches
filter (Intent.ACTION_BATTERY_CHANGED), in the main application thread.

 * Battery Related Broadcast event registration
 * */
private void registerBatteryLevelReceiver() {
    IntentFilter filter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
    registerReceiver(battery_receiver, filter);

Once registered, whenever the BroadcastReceiver is receiving an Intent
broadcast (i.e. from BatteryManager), we shall update the status as below  

 public void onReceive(Context context, Intent intent) {
     boolean isPresent = intent.getBooleanExtra("present", false);
     //Battery Technology
     String technology = intent.getStringExtra("technology");
     //Battery Plugged Information
     int plugged = intent.getIntExtra("plugged", -1);
     //Battery Scale
     int scale = intent.getIntExtra("scale", -1);
     //Battery Health
     int health = intent.getIntExtra("health", 0);
     //Battery Charging Status
     int status = intent.getIntExtra("status", 0);
     //Battery charging level
     int rawlevel = intent.getIntExtra("level", -1);
     int level = 0;
     Bundle bundle = intent.getExtras();
     Log.i("BatteryLevel", bundle.toString());
     if (isPresent) {
         if (rawlevel >= 0 && scale > 0) {
             level = (rawlevel * 100) / scale;
         String info = "Battery Level: " + level + "%\n";
         info += ("Technology: " + technology + "\n");
         info += ("Plugged: " + getPlugTypeString(plugged) + "\n");
         info += ("Health: " + getHealthString(health) + "\n");
         info += ("Status: " + getStatusString(status) + "\n");
     } else {
         setBatteryLevelText("Battery not present!!!");

More information on best practices for Monitoring the state of battery can be found here  

We are done.!! Well almost. Run the application on your Android emulator or best deploy it on your smartphone to see the status of your phone. You can play around by querying different parameters and also by customizing the way the UI is updated as i did in this case for monitoring the Data activity and signal strength.

Happy Coding !!


Initial version - 22.03.2012





This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Srivatsa Haridas
Software Developer Honeywell International
India India
Software Developer, Cinephile, Dromomaniac, Animal lover, Self proclaimed Photographer not necessarily in the same order.

You may also be interested in...

Comments and Discussions

GeneralMy vote of 5 Pin
Afzaal Ahmad Zeeshan19-Aug-15 3:19
professionalAfzaal Ahmad Zeeshan19-Aug-15 3:19 
Questionhelp me Pin
Member 105729458-May-14 2:49
memberMember 105729458-May-14 2:49 
AnswerRe: help me Pin
Srivatsa Haridas12-May-14 20:38
memberSrivatsa Haridas12-May-14 20:38 
QuestionphoneListener - cannot resolve symbol Pin
ssarna126-Apr-14 7:46
memberssarna126-Apr-14 7:46 
Questionyour code not show wifi state Pin
farhankhan098720-Mar-14 6:59
memberfarhankhan098720-Mar-14 6:59 
AnswerRe: your code not show wifi state Pin
Srivatsa Haridas21-Apr-14 21:12
memberSrivatsa Haridas21-Apr-14 21:12 
QuestionError Pin
Member 1067538316-Mar-14 22:19
memberMember 1067538316-Mar-14 22:19 
AnswerRe: Error Pin
Srivatsa Haridas21-Apr-14 21:17
memberSrivatsa Haridas21-Apr-14 21:17 
GeneralMy vote of 5 Pin
abeethadesilva15-Mar-14 3:59
memberabeethadesilva15-Mar-14 3:59 
SuggestionNeed to add extra info Pin
KK01k11a058518-Apr-13 0:53
memberKK01k11a058518-Apr-13 0:53 
GeneralRe: Need to add extra info Pin
Srivatsa Haridas18-Apr-13 9:37
memberSrivatsa Haridas18-Apr-13 9:37 
GeneralMy vote of 5 Pin
Mihai MOGA12-Apr-13 20:02
memberMihai MOGA12-Apr-13 20:02 
GeneralRe: My vote of 5 Pin
Srivatsa Haridas14-Apr-13 9:51
memberSrivatsa Haridas14-Apr-13 9:51 
GeneralMy vote of 4 Pin
tumbledDown2earth9-Apr-13 19:27
membertumbledDown2earth9-Apr-13 19:27 
GeneralMy vote of 5 Pin
Dheeraj Singh Bhadoria31-Mar-13 21:14
memberDheeraj Singh Bhadoria31-Mar-13 21:14 
GeneralMy vote of 5 Pin
SRSHINDE28-Mar-13 5:45
memberSRSHINDE28-Mar-13 5:45 
GeneralRe: My vote of 5 Pin
Srivatsa Haridas14-Apr-13 9:54
memberSrivatsa Haridas14-Apr-13 9:54 
GeneralMy vote of 5 Pin
PhillipMwaniki22-Mar-13 9:59
memberPhillipMwaniki22-Mar-13 9:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 22 Mar 2013
Article Copyright 2013 by Srivatsa Haridas
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid