Skip to main content

Android TelephonyManager Tutorial

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.   
  11.     <TextView  
  12.         android:id="@+id/textView1"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:layout_alignParentLeft="true"  
  16.         android:layout_alignParentTop="true"  
  17.         android:layout_marginLeft="38dp"  
  18.         android:layout_marginTop="30dp"  
  19.         android:text="Phone Details:" />  
  20.   
  21. </RelativeLayout>  

Activity class

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

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.javatpoint.telephonymanager"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.   
  7.     <uses-sdk  
  8.         android:minSdkVersion="8"  
  9.         android:targetSdkVersion="17" />  
  10.   
  11.     <uses-permission android:name="android.permission.READ_PHONE_STATE"/>   
  12.       
  13.     <application  
  14.         android:allowBackup="true"  
  15.         android:icon="@drawable/ic_launcher"  
  16.         android:label="@string/app_name"  
  17.         android:theme="@style/AppTheme" >  
  18.         <activity  
  19.             android:name="com.javatpoint.telephonymanager.MainActivity"  
  20.             android:label="@string/app_name" >  
  21.             <intent-filter>  
  22.                 <action android:name="android.intent.action.MAIN" />  
  23.   
  24.                 <category android:name="android.intent.category.LAUNCHER" />  
  25.             </intent-filter>  
  26.         </activity>  
  27.     </application>  
  28.   
  29. </manifest>  


Output:

android telephony manager example output 1

Comments

Popular posts from this blog

Android Tutorial

Android  is a complete set of software for mobile devices such as tablet computers, notebooks, smartphones, electronic book readers, set-top boxes etc. It contains a  linux-based Operating System ,  middleware  and  key mobile applications . It can be thought of as a mobile operating system. But it is not limited to mobile only. It is currently used in various devices such as mobiles, tablets, televisions etc. This tutorial is developed for beginners and experienced persons. Let's see the topics of android that we are going to learn. Basics of Android In this fundamental chapter, you will learn about android, its components, how to create first android application, internal of first android application etc. What is Android History and Version Software Stack Core Building Blocks Android Emulator Installing softwares Setup Eclipse Hello Android example Internal Details Dalvik VM AndroidManifest.xml R.java Hide Title Bar Activity and I...

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM

CLOUD WORKFLOW SCHEDULING WITH DEADLINE AND TIME SLOT ALGORITHM Abstract Allocating service capacities in cloud computing is based on the assumption that they are unlimited and can be used at any time. However, available service capacities change with workload and cannot satisfy users’ requests at any time from the cloud provider’s perspective because cloud services can be shared by multiple tasks. Cloud service providers provide available time slots for new user’s requests based on available capacities. In this paper, we consider workflow scheduling with deadline and time slot availability in cloud computing. An iterated heuristic framework is presented for the problem under study which mainly consists of initial solution construction, improvement, and perturbation. Three initial solution construction strategies, two greedy- and fair-based improvement strategies and a perturbation strategy are proposed. Different strategies in the three phases result in several heuristics. ...

PUNCHING MACHINE

ACCIDENT AVOIDING SYSTEM FOR PUNCHING MACHINE SYNOPSIS The aim of our project is to take a system-wide approach to preventing the machine accident. The system includes not just the machine and the operator; but rather, it includes everything from the initial design of the machine to the training of everyone that is responsible for any aspect of it, to the documentation of all changes, to regular safety audits and a finally a corporate culture of safety-first. Design is the part of a machine's life where the greatest impact can be made in relation to avoiding accidents. The designer should ensure that the machine is safe to set up and operate, safe to install, safe to maintain, safe to repair, and safe to decommission. Although safe operation is usually at the forefront of a designer's mind, safe maintenance and repair should also be a high priority. Around 50% of fatal accidents involving industrial equipment are associated with maintenance activities, and design...