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"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello Readers :)" />
    <TextView
        android:id="@+id/tv1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=" " />
</RelativeLayout>

                                                                  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;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv1 = (TextView) findViewById(R.id.tv1);
        telManager = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
        StringBuilder sb = new StringBuilder();
        sb.append("deviceid:").append(telManager.getDeviceId()).append("\n");
        sb.append("device Software Ver:")
                .append(telManager.getDeviceSoftwareVersion()).append("\n");
        sb.append("Line number:").append(telManager.getLine1Number())
                .append("\n");
        sb.append("Network Country ISO:")
                .append(telManager.getNetworkCountryIso()).append("\n");
        sb.append("Network Operator:").append(telManager.getNetworkOperator())
                .append("\n");
        sb.append("Network Operator Name:")
                .append(telManager.getNetworkOperatorName()).append("\n");
        sb.append("Sim Country ISO:").append(telManager.getSimCountryIso())
                .append("\n");
        sb.append("Sim Operator:").append(telManager.getSimOperator())
                .append("\n");
        sb.append("Sim Operator Name:").append(telManager.getSimOperatorName())
                .append("\n");
        sb.append("Sim Serial Number:").append(telManager.getSimSerialNumber())
                .append("\n");
        sb.append("Subscriber Id:").append(telManager.getSubscriberId())
                .append("\n");
        sb.append("Voice Mail Alpha Tag:")
                .append(telManager.getVoiceMailAlphaTag()).append("\n");
        sb.append("Voice Mail Number:").append(telManager.getVoiceMailNumber())
                .append("\n");
        tv1.setText(sb.toString());
    }
    @Override
    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"
    package="com.android.tution.Telephony"
    android:versionCode="1"
    android:versionName="1.0" >
    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.android.tution.Telephony.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

                                                             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!!!