How to Get Information About Phone Using TelePhonyManager in Android

Introduction

 
This article explains how to get information about a phone using TelePhonyManager in Android. Android Studio is used to create the sample.
 
This application will show all the phone details in your activity using telephonyManager. To get all the details you need to create an object of TelephonyManager. The TelePhonyManager class provides methods that will return all the details. You need to also give permission to the Android Manifest.xml file.
 
Methods of TelePhonyManager class
  • getDeviceId() gets device id that is of String type.
  • getSimSerialNumber() gets the simserial number that is of String type.
  • getNetworkCountryIso() gets the network country ISO that is of String type.
  • getSimCountryIso() gets the sim country ISO that is of String type.
  • getDeviceSoftwareVersion() gets the Device Country ISO that is of String type.
  • getVoiceMailNumber() gets the voice mail number that is of string type.
  • getSimOPeration number() returns the sim operation number that is of String type.
  • getNetwork() gets the network that is Integer type.
  • getPhoneType() gets the phonetype that is of integer type.
Use the following permission in the Android Manifest.xml file:
  1. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> 
Step 1
 
Create a project like this:  
 
imageentervalue
 
Step 2
 
Create an XML file with the following:
  1. <RelativeLayout xmlns:android="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:paddingLeft="@dimen/activity_horizontal_margin"  
  6.     android:paddingRight="@dimen/activity_horizontal_margin"  
  7.     android:paddingTop="@dimen/activity_vertical_margin"  
  8.     android:paddingBottom="@dimen/activity_vertical_margin"  
  9.     tools:context=".MainActivity">  
  10.    
  11.     <TextView  
  12.         android:id="@+id/textView"  
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:textSize="15dp"  
  16.         android:text="Phone Details"  
  17.         android:layout_centerHorizontal="true"  
  18.         />  
  19. </RelativeLayout> 
Step 3
 
Create a Java file with the following.
 
In this, you will create a TelePhonyManager object and call the methods regarding the details you want to show on the activity.
  1. package com.androidtelephonymanagerexample;  
  2.    
  3. import android.content.Context;  
  4. import android.os.Bundle;  
  5. import android.app.Activity;  
  6. import android.telephony.TelephonyManager;  
  7. import android.view.Menu;  
  8. import android.widget.TextView;  
  9.    
  10. public class MainActivity extends Activity {  
  11.    
  12.     TextView textView;  
  13.     String setType;  
  14.    
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.         textView = (TextView) findViewById(R.id.textView);  
  20.         
  21.         TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);  
  22.         
  23.         String deviceId = telephonyManager.getDeviceId();  
  24.         String simSerialNumber = telephonyManager.getSimSerialNumber();  
  25.         String networkCountryIso = telephonyManager.getNetworkCountryIso();  
  26.         String simCountryIso = telephonyManager.getSimCountryIso();  
  27.         String deviceSoftwareVersion = telephonyManager.getDeviceSoftwareVersion();  
  28.         String voiceMailNumber = telephonyManager.getVoiceMailNumber();  
  29.         String simOperatorName = telephonyManager.getSimOperatorName();  
  30.         int networkType = telephonyManager.getNetworkType();  
  31.    
  32.    
  33.         if(telephonyManager.getPhoneType()==1)  
  34.         {  
  35.           setType="GSM";  
  36.         }  
  37.         textView.setText("deviceid:" + deviceId + "" + "\n simSerialNumber:" + simSerialNumber + "" + " \n networkCountryIso :" + networkCountryIso + "" + "\n simCountryIso :" + simCountryIso +  
  38.                 "" + "\n deviceSoftwareVersion :" + deviceSoftwareVersion + "" + "\n voiceMailNumber :" + voiceMailNumber + "" + "\nNetworkType: " + networkType + "" + "\nsimOperatorName: " +  
  39.                 simOperatorName+""+"\nphoneType:"+setType+""+"\nnetworkType:"+networkType);  
  40.     }  
  41.     @Override  
  42.     public boolean onCreateOptionsMenu(Menu menu) {  
  43.         // Inflate the menu; this adds items to the action bar if it is present.  
  44.         getMenuInflater().inflate(R.menu.main, menu);  
  45.         return true;  
  46.     }  
Step 4
 
Provide the following permission in the Android Manifest.xml file:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.androidtelephonymanagerexample"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.    
  7.     <uses-sdk  
  8.         android:minSdkVersion="7"  
  9.         android:targetSdkVersion="16" />  
  10.    
  11.     <application  
  12.         android:allowBackup="true"  
  13.         android:icon="@drawable/ic_launcher"  
  14.         android:label="@string/app_name"  
  15.         android:theme="@style/AppTheme" >  
  16.         <activity  
  17.             android:name="com.androidtelephonymanagerexample.MainActivity"  
  18.             android:label="@string/app_name" >  
  19.             <intent-filter>  
  20.                 <action android:name="android.intent.action.MAIN" />  
  21.    
  22.                 <category android:name="android.intent.category.LAUNCHER" />  
  23.             </intent-filter>  
  24.         </activity>  
  25.     </application>  
  26. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
  27. </manifest> 
Step 5
 
All the details regarding the phone:
 
image which show daata


Similar Articles