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

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search

Inverted Linear Quadtree: Efficient Top K Spatial Keyword Search ABSTRACT: In this paper, With advances in geo-positioning technologies and geo-location services, there are a rapidly growing amount of spatiotextual objects collected in many applications such as location based services and social networks, in which an object is described by its spatial location and a set of keywords (terms). Consequently, the study of spatial keyword search which explores both location and textual description of the objects has attracted great attention from the commercial organizations and research communities. In the paper, we study two fundamental problems in the spatial keyword queries: top k spatial keyword search (TOPK-SK), and batch top k spatial keyword search (BTOPK-SK). Given a set of spatio-textual objects, a query location and a set of query keywords, the TOPK-SK retrieves the closest k objects each of which contains all keywords in the query. BTOPK-SK is the batch processing of sets...

A simple and reliable touch sensitive security system CODING

#include <REGX51.H> #include "lcd.c" #define MAX_DELAY() delay(65000) sbit Vibra_Sense=P3^1; sbit Buz=P1^0; void intro() {  lcd_init();  lcd_str("Touch Sensitive ",0x80);  lcd_str("Security System ",0xc0);  MAX_DELAY();MAX_DELAY();  lcd_clr();  }  void main()  { unsigned int i = 0, j= 0; intro();    while(1)    { lcd_str("Security Syst On",0x80); lcd_str("No Vibra Detectd",0xc0); Buz = 1; if(Vibra_Sense == 1) { while(Vibra_Sense == 1) delay(1000); } else { while(Vibra_Sense == 0) delay(1000); } Buz = 0; lcd_str("Vibraton Detectd",0xc0);delay(65000); while(1);    }  }

A Time Efficient Approach for Detecting Errors in Big Sensor Data on Cloud

A Time Efficient Approach for Detecting Errors in Big Sensor Data on Cloud Abstract                                                                                                                                                      ...