Via : OpenAndroid.in
Tags : #Downloads, #Source_Code, #Tutorial
Orginal Post: How To Handle Incoming Calls in Android

call handle


Handle Incoming Calls in Android


In this Post I will describe how to Handle incoming Calls on Android and Reacting to them i.e. receiving, rejecting etc.

We  can do this by Activity and by Broadcast Receiver, here we will do using Activity.

Permissions:
As I mentioned in my earlier posts we require to declare permission in manifest file.
For listening and reacting to Incoming calls we need following permissions

android.permission.READ_PHONE_STATE   to listen to InComing Calls
"android.permission.MODIFY_PHONE_STATE"   to receive Incoming Calls

You can learn more about permission  Here.

Add these permissions in your manifest file.
Your Manifest file should look like

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

<uses-sdk android:minSdkVersion="8"
android:targetSdkVersion="15" />

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


 <application
android:icon="@drawable/icon"
android:label="@string/app_name" >
<activity
android:name=".HomeActivity"
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>



Activity:

We need to use TelephonyManager class and  TELEPHONY_SERVICE.

The onCallStateChanged handler receives the phone number associated with incoming calls, and the
state parameter represents the current call state as one of the following three values:

➤ TelephonyManager.CALL_STATE_RINGING When the phone is ringing




 







➤ TelephonyManager.CALL_STATE_OFFHOOK When the phone is currently in a call means call is recieved









➤ TelephonyManager.CALL_STATE_IDLE When the phone is neither ringing nor in a call





How to Monitor Incoming Calls:


We need to have a listener to listen the InComing Calls

PhoneStateListener callStateListener = new PhoneStateListener()
public void onCallStateChanged(int state, String incomingNumber)
// TODO React to incoming call.

;
telephonyManager.listen(callStateListener,
PhoneStateListener.LISTEN_CALL_STATE);

Code:
public class GetCallerInfoActivity extends Activity


@Override
public void onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.main);

TelephonyManager telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
 // register PhoneStateListener 
PhoneStateListener callStateListener = new PhoneStateListener()
public void onCallStateChanged(int state, String incomingNumber)

//  React to incoming call.
number=incomingNumber;
// If phone ringing
 if(state==TelephonyManager.CALL_STATE_RINGING)

Toast.makeText(getApplicationContext(),"Phone Is Riging", Toast.LENGTH_LONG).show();


// If incoming call received
 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);







How To Launch the Dialer to Initiate Phone Calls:


Use an Intent action to start a dialer activity; you should specify the number to dial using the tel: schema as the data component of the Intent.

Use the Intent.ACTION_DIAL Activity action to launch a dialer rather than dial the number immediately.
This action starts a dialer Activity, passing in the specified number but allowing the dialer application
to manage the call initialization (the default dialer asks the user to explicitly initiate the call). This
action doesn’t require any permissions and is the standard way applications should initiate calls.

Intent intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:1234567"));
startActivity(intent); 


Via : OpenAndroid.in
Tags : #Downloads, #Source_Code, #Tutorial
Orginal Post: How To Handle Incoming Calls in Android

0 comments:

Post a Comment

 
Top