11.1 Android Telephony Hardware

Hello Readers!! Telephony hardware is the first sub-section we are going to cover under telephone services. Hope you will enjoy this section. In this section, we shall cover a small example and few basics about the telephony services provided by android. Let us not waste time in paraphrasing and start doing this section.

11.1.1 Introduction

A mobile handset is a state machine. It keeps track of the mobile radio reports, provides audible call state indications to the user, and enables the user to provide inputs which modifies that state.

In android environment, the android.telephony package contains a set of classes. These classes can be used by any application to monitor the state of Android’s mobile network connection. This package also contains classes for locating a device via the intelligent usage of mobile network. It also offers utility classes for parsing, formatting, and managing phone numbers. To be honest there is no architectural benefit to locating those classes in this package. Telephony package focuses on retrieving information and giving users an interface to edit telephone numbers.

The constituent components of this package are listed below:

  • CellLocation: It contains the methods to request location information.
  • PhoneNumberFormattingTextWatcher: It consists the callback methods which notifies an application about the changes to phone numbers themselves. It can be used with a TextView object. In that case it formats the text as a phone number using methods in the PhoneNumberUtilsclass.
  • PhoneNumberUtils: This is a utility class which contains methods for processing strings containing phone numbers.
  • PhoneStateListener: This contains the callback methods which are responsible for tracking changes in the state of the mobile network connection,the call, or other telephony objects.
  • ServiceState: These are the methods which return information about the current mobile network service provider. They also provide information about the availability of service.
  • TelephonyManager: This consists of methods which provide information about the state of mobile service, the call state, the SIM, the network, the subscriber to the mobile service, voicemail, and other related information.

Let us create a small example and find out the new friend called telephony.

11.1.2 Example

Create a project and name it as you like. I am naming it as TelephonySample application. This is going to display the phone status. Open the activity_main.xml file and code it as shown in the following listing:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >
        android:text="Hello Readers :)" />
        android:text=" " />

                                                                  Figure activity_main.xml

The graphical layout of application would be as shown in the following snapshot:

Figure Graphical snapshot of app

Open your main activity file and code it as shown in the following listing:


package com.android.tution.Telephony;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.widget.TextView;
public class MainActivity extends Activity {
    TextView tv1;
    TelephonyManager telManager;
    protected void onCreate(Bundle savedInstanceState) {
        tv1 = (TextView) findViewById(R.id.tv1);
        telManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        StringBuilder sb = new StringBuilder();
        sb.append("device Software Ver:")
        sb.append("Line number:").append(telManager.getLine1Number())
        sb.append("Network Country ISO:")
        sb.append("Network Operator:").append(telManager.getNetworkOperator())
        sb.append("Network Operator Name:")
        sb.append("Sim Country ISO:").append(telManager.getSimCountryIso())
        sb.append("Sim Operator:").append(telManager.getSimOperator())
        sb.append("Sim Operator Name:").append(telManager.getSimOperatorName())
        sb.append("Sim Serial Number:").append(telManager.getSimSerialNumber())
        sb.append("Subscriber Id:").append(telManager.getSubscriberId())
        sb.append("Voice Mail Alpha Tag:")
        sb.append("Voice Mail Number:").append(telManager.getVoiceMailNumber())
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;

                                                                                        Figure activity file of app

Our last task is to code our manifest file. Open manifest and populate it as shown in the following listing:


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    android:versionName="1.0" >
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
        android:theme="@style/AppTheme" >
            android:label="@string/app_name" >
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />

                                                             Figure manifest file

Create a suitable emulator and run the application. Output should look similar to the following snapshot:

Figure Output of TelephonySample app

Congratulations ladies and gentlemen!!! We have successfully completed this sub-section. See you in the next section we something new and interesting. Till then keep practicing. Happy App Developing!!!