Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Get Call State


We can also get the information of call state using the TelephonyManager class. For this purpose, we need to call the listen method of TelephonyManager class by passing the PhonStateListener instance.

The PhoneStateListener interface must be implemented to get the call state. It provides one method onCallStateChanged().

Android Call State Example

Let’s see the example, where we are determining whether phone is ringing or phone is in a call or phone is neither ringing nor in a call.

activity_main.xml

In this example, we don’t have any component in this file..


Activity class

Let’s write the code to know the call state.

File: MainActivity.java
  1. package com.Kreationnext.callstates;
  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.telephony.PhoneStateListener;
  6. import android.telephony.TelephonyManager;
  7. import android.view.Menu;
  8. import android.widget.Toast;
  9. public class MainActivity extends Activity {
  10.     @Override
  11.     protected void onCreate(Bundle savedInstanceState) {
  12.         super.onCreate(savedInstanceState);
  13.         setContentView(R.layout.activity_main);
  14.         TelephonyManager telephonyManager =
  15.                       (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
  16.         PhoneStateListener callStateListener = new PhoneStateListener() {
  17.         public void onCallStateChanged(int state, String incomingNumber)
  18.         {
  19.               if(state==TelephonyManager.CALL_STATE_RINGING){
  20.                         Toast.makeText(getApplicationContext(),“Phone Is Riging”,
  21.                                                                          Toast.LENGTH_LONG).show();
  22.               }
  23.                 if(state==TelephonyManager.CALL_STATE_OFFHOOK){
  24.                     Toast.makeText(getApplicationContext(),“Phone is Currently in A call”,
  25.                                                                                   Toast.LENGTH_LONG).show();
  26.                 }
  27.                 if(state==TelephonyManager.CALL_STATE_IDLE){
  28.                     Toast.makeText(getApplicationContext(),“phone is neither ringing nor in a call”,
  29.                                                                                                  Toast.LENGTH_LONG).show();
  30.                 }
  31.         }
  32.         };
  33.         telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);
  34.     }
  35.     @Override
  36.     public boolean onCreateOptionsMenu(Menu menu) {
  37.         // Inflate the menu; this adds items to the action bar if it is present.
  38.         getMenuInflater().inflate(R.menu.main, menu);
  39.         return true;
  40.     }
  41. }

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

Output:

callstate1

callstate2

Comments

comments