outgoing call detect in PhoneStateListener - android

I want to detect outgoing call in android application . Actually I can detect outgoing calls but it is also working for incoming too and I don't want that. I need it to work only for outgoing. here is my code....
boolean ringing = false;
boolean offhook = false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
if((!ringing)&& offhook){
///incomming call
}
break;
case TelephonyManager.CALL_STATE_RINGING:
callerPhoneNumber = incomingNumber;
Log.d(TAG, "RINGING");
ringing = true;
offhook = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(TAG, "OFFHOOK");
offhook = true;
ringing = false;
break;
}

You can write a broadcast receiver for android.intent.action.NEW_OUTGOING_CALL.
See here Sample code is available

Related

How to listen with PhoneStateListener on dual sim?

I am new to android development. trying to make a simple call recorder.
The following PhoneStateListner is working perfectly for sim2 incoming and outgoing calls. but call state CALL_STATE_IDLE is being called right after CALL_STATE_OFFHOOK when an incoming/outcoming call event occurs in SIM1.
AtomicBoolean isRecording = new AtomicBoolean();
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE: // Idle... no call
if (isRecording.get()) {
RecordCallService.stopRecording(context);
phoneCall = null;
isRecording.set(false);
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK: // Call answered
if(!isRecording.get())
RecordCallService.startRecording(context, phoneCall);
}
break;
case TelephonyManager.CALL_STATE_RINGING:
if (null == phoneCall)
phoneCall = new CallLog();
}
break;
}
}

Stop tts if the call is in offHook

I am doing an application which speaks the caller name after 4 sec of the ring tone.The problem is it speaks the caller Name after two or three rings. The problem is it keeps speaking the caller name even if the call is in offhook or idle I don't want so.
the code in onreceive of broadcast receiver is
state = bundle.getString(TelephonyManager.EXTRA_STATE);
if(state.equals( TelephonyManager.CALL_STATE_OFFHOOK))
{
System.out.println("fjkerj");
}
else if(state.equals( TelephonyManager.CALL_STATE_IDLE))
{
}
else if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
System.out.println("Entered Receiver");
final String phonenumber = bundle.getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
// String info = "Detect Calls sample application\nIncoming number: " + phonenumber;
AudioManager amanager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
switch (amanager.getRingerMode()) {
case AudioManager.RINGER_MODE_SILENT:
System.out.println("Silent");
break;
case AudioManager.RINGER_MODE_VIBRATE:
System.out.println("Vibrate");
break;
case AudioManager.RINGER_MODE_NORMAL:
amanager.setStreamVolume(AudioManager.STREAM_RING,1, 0);
Handler handler=new Handler();
Runnable r=new Runnable()
{
public void run()
{
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))
{
Intent IncomingNumberIntent = new Intent(context1,SpeakOut.class);
IncomingNumberIntent.putExtra("PhoneNumber", phonenumber);
context1.startService(IncomingNumberIntent);
}
}
};
handler.postDelayed(r, 3000);
If the call is in ringing and profile is normal, it will go to a service and talks the name but
the trouble that app speaks the name even if the call is offhook.
Change your code to
int state = bundle.getInt(TelephonyManager.EXTRA_STATE);
switch (state)
{
case TelephonyManager.CALL_STATE_OFFHOOK:
System.out.println("fjkerj");
break;
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
// Your code goes here
}
Remove from the run() method the if statement
if(state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING))

How to check phone service state in android

I am developing an app in which i am trying to check the service state of phone
using Service state: IN_SERVICE
if the phone is IN_SERVICE generate a Toast phone is in service if its not then generate a Toast phone is not in service
im trying this code
public class MyphoneStateListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
break;
case TelephonyManager.CALL_STATE_RINGING:
sendemail();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
default:
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
not able to get the Service state: IN_SERVICE Please help me
Thanks in advance
TelephonyManager telMng = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
telMng.listen(new PhoneStateListener() {
#Override
public void onServiceStateChanged (ServiceState serviceState) {
super.onServiceStateChanged(serviceState);
String phonestate;
switch(serviceState.getState()) {
case ServiceState.STATE_EMERGENCY_ONLY:
phonestate ="STATE_EMERGENCY_ONLY";
break;
case ServiceState.STATE_IN_SERVICE:
phonestate ="STATE_IN_SERVICE";
break;
case ServiceState.STATE_OUT_OF_SERVICE:
phonestate ="STATE_OUT_OF_SERVICE";
break;
case ServiceState.STATE_POWER_OFF:
phonestate ="STATE_POWER_OFF";
break;
default:
phonestate = "Unknown";
break;
}
}
}, PhoneStateListener.LISTEN_SERVICE_STATE);

How to identify the Rejected call and Missed call in android?

How to identify the Rejected calls and Missed calls by using PhoneStateListener? In my code i am sending sms only rejected calls not a missed calls. Please verify below code.
class PhoneStateChangeListener extends PhoneStateListener{
public static boolean wasRinging;
SmsManager smsManager = SmsManager.getDefault();
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.e("pppp", "RINGING");
// Ringing
wasRinging=false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.e("Lifttttttt", "OFFHOOK");
//Received calls
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.e("Endddddddd", "IDLE");
if(!wasRinging ){
//Missed or Rejected calls
}
wasRinging = true;
break;
}
super.onCallStateChanged(state, incomingNumber);
}
}
Edit: Is it possible or not? please give to replay. Thanks.

Streaming issue in android

I have streamed audio from url,but when the call or sms comes i need to pause the song and when the call ends again i am gonna resume the song...
How to do this?...
This method works in my app, it's pretty simple. In your onCreate() put this code to listen for phone events and then act appropriately:
TelephonyManager telephonyManager;
PhoneStateListener ringListener;
// Get the telephony manager
telephonyManager = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
// Create a new PhoneStateListener
ringListener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
String stateString = "none";
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
stateString = "Idle";
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
stateString = "Off Hook";
break;
case TelephonyManager.CALL_STATE_RINGING:
stateString = "Ringing";
Log.i(TAG, "TELEPHONE RINGING!!");
//Do stuff here when phone ringing detected
break;
}
}
};
// Register the listener with the telephony manager
telephonyManager.listen(ringListener, PhoneStateListener.LISTEN_CALL_STATE);
//End create a new PhoneStateListener

Categories

Resources