Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

TelephonyManager


The android.telephony.TelephonyManager class provides information about the telephony services such as subscriber id, sim serial number, phone network type etc. Moreover, you can determine the phone state etc.

Android TelephonyManager Example

Let’s see the simple example of TelephonyManager that prints information of the telephony services.

activity_main.xml

Drag one textview from the pallete, now the xml file will look like this.

File: activity_main.xml
  1. <RelativeLayout xmlns:androclass=“http://schemas.android.com/apk/res/android”
  2.     xmlns:tools=“http://schemas.android.com/tools”
  3.     android:layout_width=“match_parent”
  4.     android:layout_height=“match_parent”
  5.     android:paddingBottom=“@dimen/activity_vertical_margin”
  6.     android:paddingLeft=“@dimen/activity_horizontal_margin”
  7.     android:paddingRight=“@dimen/activity_horizontal_margin”
  8.     android:paddingTop=“@dimen/activity_vertical_margin”
  9.     tools:context=“.MainActivity” >
  10.     <TextView
  11.         android:id=“@+id/textView1”
  12.         android:layout_width=“wrap_content”
  13.         android:layout_height=“wrap_content”
  14.         android:layout_alignParentLeft=“true”
  15.         android:layout_alignParentTop=“true”
  16.         android:layout_marginLeft=“38dp”
  17.         android:layout_marginTop=“30dp”
  18.         android:text=“Phone Details:” />
  19. </RelativeLayout>

Activity class

Now, write the code to display the information about the telephony services.

File: MainActivity.java
  1. package com.Kreationnext.telephonymanager;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.telephony.TelephonyManager;
  6. import android.view.Menu;
  7. import android.widget.TextView;
  8. public class MainActivity extends Activity {
  9.    TextView textView1;
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.         textView1=(TextView)findViewById(R.id.textView1);
  15.         //Get the instance of TelephonyManager
  16.         TelephonyManager  tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  17.         //Calling the methods of TelephonyManager the returns the information
  18.         String IMEINumber=tm.getDeviceId();
  19.         String subscriberID=tm.getDeviceId();
  20.         String SIMSerialNumber=tm.getSimSerialNumber();
  21.         String networkCountryISO=tm.getNetworkCountryIso();
  22.         String SIMCountryISO=tm.getSimCountryIso();
  23.         String softwareVersion=tm.getDeviceSoftwareVersion();
  24.         String voiceMailNumber=tm.getVoiceMailNumber();
  25.         //Get the phone type
  26.         String strphoneType=“”;
  27.         int phoneType=tm.getPhoneType();
  28.         switch (phoneType)
  29.         {
  30.                 case (TelephonyManager.PHONE_TYPE_CDMA):
  31.                            strphoneType=“CDMA”;
  32.                                break;
  33.                 case (TelephonyManager.PHONE_TYPE_GSM):
  34.                            strphoneType=“GSM”;
  35.                                break;
  36.                 case (TelephonyManager.PHONE_TYPE_NONE):
  37.                             strphoneType=“NONE”;
  38.                                 break;
  39.          }
  40.         //getting information if phone is in roaming
  41.         boolean isRoaming=tm.isNetworkRoaming();
  42.         String info=“Phone Details:\n”;
  43.         info+=“\n IMEI Number:”+IMEINumber;
  44.         info+=“\n SubscriberID:”+subscriberID;
  45.         info+=“\n Sim Serial Number:”+SIMSerialNumber;
  46.         info+=“\n Network Country ISO:”+networkCountryISO;
  47.         info+=“\n SIM Country ISO:”+SIMCountryISO;
  48.         info+=“\n Software Version:”+softwareVersion;
  49.         info+=“\n Voice Mail Number:”+voiceMailNumber;
  50.         info+=“\n Phone Network Type:”+strphoneType;
  51.         info+=“\n In Roaming? :”+isRoaming;
  52.         textView1.setText(info);//displaying the information in the textView
  53.     }
  54. }

AndroidManifest.xml

You need to provide READ_PHONE_STATE permission in the AndroidManifest.xml file.

File: AndroidManifest.xml
  1. <?xml version=“1.0” encoding=“utf-8”?>
  2. <manifest xmlns:androclass=“http://schemas.android.com/apk/res/android”
  3.     package=“com.Kreationnext.telephonymanager”
  4.     android:versionCode=“1”
  5.     android:versionName=“1.0” >
  6.     <uses-sdk
  7.         android:minSdkVersion=“8”
  8.         android:targetSdkVersion=“17” />
  9.     <uses-permission android:name=“android.permission.READ_PHONE_STATE”/>
  10.     <application
  11.         android:allowBackup=“true”
  12.         android:icon=“@drawable/ic_launcher”
  13.         android:label=“@string/app_name”
  14.         android:theme=“@style/AppTheme” >
  15.         <activity
  16.             android:name=“com.Kreationnext.telephonymanager.MainActivity”
  17.             android:label=“@string/app_name” >
  18.             <intent-filter>
  19.                 <action android:name=“android.intent.action.MAIN” />
  20.                 <category android:name=“android.intent.category.LAUNCHER” />
  21.             </intent-filter>
  22.         </activity>
  23.     </application>
  24. </manifest>

download this android example

Output:

telephony1

Comments

comments