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

Jio

Reliance Jio planning its own  cryptocurrency called JioCoin  elder son Akash Ambani leading the JioCoin project, Reliance Jio plans to build a 50-member team of young professionals to work on blockchain technology, which can also be used to develop applications such as smart contracts and supply chain management logistics

garbage monitoring using arduino code with gsm

#include <SoftwareSerial.h> #include <LiquidCrystal.h> //LiquidCrystal lcd(7, 6, 5, 4, 3, 2); LiquidCrystal lcd(13, 12, 11, 10, 9, 8); SoftwareSerial mySerial(0,1); #define trigPin 2 #define echoPin 3 #define PIR_sensor 4 #define m11 5 #define m12 6 void setup() {    lcd.begin(16, 2);    lcd.print("    Garbage    ");    Serial.println("garbage ");   lcd.setCursor(0,1);   lcd.print("   Open Close    ");   Serial.println(" open close");   delay(3000);   lcd.clear();   //lcd.print(" ");   delay(2000); mySerial.begin(9600); // Setting the baud rate of GSM Module Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT);  pinMode(m11, OUTPUT);   pinMode(m12, OUTPUT);   } void loop() {  readPIR();  pingDist();  SendMessage(); } void pingDist ()   {     long duration, distance;...