Site icon WebArtDeveloper

How to Use TelephonyManager Services in Android

In this tutorial how to use TelephonyManager Services in Android Application is shown.

Telephony Manager provides access of information about the telephony services on the device.

Applications can use the methods in this class to determine telephony services and states,as well as to access some types of subscriber information.

Applications can also register a listener to receive notification of telephony state changes.

You do not instantiate this class directly, instead, you retrieve a reference to an instance through Context.getSystemService(Context.TELEPHONY_SERVICE);

To work with TelephonyManager and to read the phone details we need to add this permission statement in AndroidManifest.xml

<uses-permission android:name=”android.permission.READ_PHONE_STATE”/>

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<Button
android:id=”@+id/button1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”76dp”
android:onClick=”onSubmit”
android:text=”Get Info” />

<TextView
android:id=”@+id/info”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_below=”@+id/button1″
android:layout_centerHorizontal=”true”
android:layout_marginTop=”47dp”
android:text=”Mobile Information” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity{

TextView tv;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.info);
}

public void onSubmit(View v)
{
TelephonyManager tm=(TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
int callstate=tm.getCallState();
String callstat=””;
switch(callstate)
{
case TelephonyManager.CALL_STATE_IDLE:
callstat=”Call State: Phone is idle\n”;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
callstat=”Call State: Phone is in use\n”;
break;
case TelephonyManager.CALL_STATE_RINGING:
callstat=”Call State: Phone is ringing\n”;
break;
}
String opname=”\nOperator ID:”+” “+tm.getNetworkOperator();
opname=opname+”\nOperator Name:”+” “+tm.getNetworkOperatorName();

int phoneType=tm.getPhoneType();
String ptype=””;
switch(phoneType)
{
case TelephonyManager.PHONE_TYPE_CDMA:
ptype=”\nPhone Type: CDMA\n”;
break;
case TelephonyManager.PHONE_TYPE_GSM:
ptype=”\nPhone Type: GSM\n”;
break;
case TelephonyManager.PHONE_TYPE_SIP:
ptype=”\nPhone Type: SIP\n”;
break;
case TelephonyManager.PHONE_TYPE_NONE:
ptype=”\nPhone Type: NONE\n”;
break;
}

boolean isRoaming=tm.isNetworkRoaming();
String pDetails=””;
if(isRoaming)
{
pDetails=”Roaming : Yes\n”;
}
else
{
pDetails=”Roaming : No\n”;
}

int sim=tm.getSimState();
String sstate=””;
switch(sim)
{
case TelephonyManager.SIM_STATE_ABSENT:
sstate=”Sim State: Absent\n”;
break;
case TelephonyManager.SIM_STATE_NETWORK_LOCKED:
sstate=”Sim State: Network Locked\n”;
break;
case TelephonyManager.SIM_STATE_PIN_REQUIRED:
sstate=”Sim State: Pin Required\n”;
break;
case TelephonyManager.SIM_STATE_PUK_REQUIRED:
sstate=”Sim State: Puk Required\n”;
break;
case TelephonyManager.SIM_STATE_READY:
sstate=”Sim State: Ready\n”;
break;
case TelephonyManager.SIM_STATE_UNKNOWN:
sstate=”Sim State: Unknown\n”;
break;
}
tv.setText(callstat+opname+ptype+pDetails+sstate);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />
<uses-permission android:name=”android.permission.READ_PHONE_STATE”/>

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

https://youtu.be/a1G3SjYTKHM

Exit mobile version