How can I differentiate between incoming and outgoing calls ?. for both cases, the mobile is in an off-hook state, but for the incoming call it is in the ringing state then goes to the off-hook one,
I tried this code but it's not working effectively, as a ringing variable is always false. and there is another question how can I get the outgoing call number .. thanks in advance
public class states extends BroadcastReceiver {
boolean Ringing = false;
public void onReceive(Context context, Intent intent) {
String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {
Toast.makeText(context, "Ringing", Toast.LENGTH_SHORT).show();
Ringing = true ;
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
if (Ringing) {
// do nothing
}else{
// do something
}
else if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_IDLE)) {
Ringing = false ;
}
}
}
these are my permissions
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<receiver android:name=".states">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
For differentiation of incoming and outgoing Calls you can use a broadcast Receiver for this :
add <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
in your receiver and process it in your onReceive() method.
<receiver android:name=".IncomingCallReceiver" >
<intent-filter >
<action android:name="android.intent.action.PHONE_STATE" />
<action android:name="android.intent.action.NEW_OUTGOING_CALL" />
</intent-filter>
</receiver>
For getting the outgoing number
public void onReceive(Context context, Intent intent)
{
String state=intent.getStringExtra(TelephonyManager.EXTRA_STATE);
if(state==null)
{
//Outgoing call
String number=intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
Log.i("tag","Outgoing number : "+number);
}
else if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
{
//Incoming call
String number = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);
Log.i("tag","Incoming number : "+number);
}
}