Android TelephonyManager Example Code

Android Call State Example


We can likewise get the data of call state utilizing the TelephonyManager class. For this reason, we have to call the listen technique for TelephonyManager class by passing the PhonStateListener occasion. 

The PhoneStateListener interface must be executed to get the call state. It gives one strategy onCallStateChanged().


Android Call State Example
We should see the illustration, where we are figuring out if telephone is ringing or telephone is in a call or telephone is neither ringing nor in a call.


activity_main.xml
In this sample, we don't have any part in this record..

File: MainActivity.java
package com.dheeruapps.callstates;  
  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Context;  
import android.telephony.PhoneStateListener;  
import android.telephony.TelephonyManager;  
import android.view.Menu;  
import android.widget.Toast;  
  
public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
          
        TelephonyManager telephonyManager =  
                      (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);  
         
        PhoneStateListener callStateListener = new PhoneStateListener() {  
        public void onCallStateChanged(int state, String incomingNumber)   
        {  
              if(state==TelephonyManager.CALL_STATE_RINGING){  
                        Toast.makeText(getApplicationContext(),"Phone Is Riging",  
                                                                         Toast.LENGTH_LONG).show();  
              }  
                if(state==TelephonyManager.CALL_STATE_OFFHOOK){  
                    Toast.makeText(getApplicationContext(),"Phone is Currently in A call",   
                                                                                  Toast.LENGTH_LONG).show();  
                }  
                                  
                if(state==TelephonyManager.CALL_STATE_IDLE){  
                    Toast.makeText(getApplicationContext(),"phone is neither ringing nor in a call",  
                                                                                                 Toast.LENGTH_LONG).show();  
                }  
        }  
        };  
        telephonyManager.listen(callStateListener,PhoneStateListener.LISTEN_CALL_STATE);  
          
    }  
  
    @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

You have to give READ_PHONE_STATE authorization in the AndroidManifest.xml record.

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




0 comments:

Post a Comment

My Instagram