Based on these three states:
TelephonyManager.CALL_STATE_IDLE
TelephonyManager.CALL_STATE_OFFHOOK
TelephonyManager.CALL_STATE_RINGING:
Is it possible to tell if there is an incoming our outgoing call?
Specifically, if there is an incoming call,
Is there a state for when user answers the call?
A state for when the call ends?
Are there similar states for outgoing calls?
Also, is there a state for rejecting a call?
You should use your own class extending PhoneStateListener to handle when the call state changes :
CallStateListener callListener= new CallStateListener ();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
Then, the following code for your own class :
public class CallStateListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_IDLE");
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_OFFHOOK");
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d(CallStateListener.class.getSimpleName(), "CALL_STATE_RINGING");
break;
}
}
}
Related
Can I get the number that is calling by native dialer ?
I want to show the "toast message" with information about cost per minute according to a dialing number. But I do not know how to get this number.
It could be good to have sample which show "toast message" with dialed number during of call.
yes you can do it.You need to implement PhoneStateListener
public class CustomPhoneStateListener extends PhoneStateListener {
public static Boolean phoneRinging = false;
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("DEBUG", "IDLE");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("DEBUG", "OFFHOOK");
phoneRinging = false;
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("DEBUG", "RINGING");
phoneRinging = true;
break;
}
}
}
To register the listener do this:
CustomPhoneStateListener phoneListener = new CustomPhoneStateListener();
telephony = (TelephonyManager) context
.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
Note that access to some telephony information is permission-protected. Your application won't receive updates for protected information unless it has the appropriate permissions declared in its manifest file. Where permissions apply, they are noted in the appropriate LISTEN_ flags.
I'm developing on android framework,
I want to fire an event when an outgoing call is received by the callee , and also when the call is ended (from any of the two sides)
Inorder to know wheter the calling party has recieved the call, you will need to create a listener.
class PhoneInfo extends BroadcastReceiver {
/**
* Getting the System Telephony Service and registering a listener for Voice Call state
*/
#Override
public void onReceive(Context context, Intent intent) {
IncomingCallListener phoneListener = new IncomingCallListener();
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
class IncomingCallListener extends PhoneStateListener {
public void onCallStateChanged(int state, String incomingNumber) {
Log.i(logcat,"CALL_STATE changed " + callflag);
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.i(logcat,"CALL_STATE_IDLE");
//This is where call ends.
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//This is where we know call is established
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.i(logcat,"CALL_STATE_RINGING");
break;
}
}
}
Register this with your activity as
phoneInfo = new PhoneInfo(this);
registerReceiver(phoneInfo, new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL));
Now with Logs u can see how states are changed when a call is dialed or received.
I am trying to know how to alert when the callee lifts the call. I have used PhoneStateListener along with BroadcastReceiver.
Generally it has three states CALL_STATE_IDLE , CALL_STATE_OFFHOOK, CALL_STATE_RINGING.
CALL_STATE_OFFHOOK state was calling when call is connecting, No state of the above three states was called after callee answered
call.
Here is my BroadcastReceiver.
public class PhoneStateBroadcastReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(new CustomPhoneStateListener(context), PhoneStateListener.LISTEN_CALL_STATE);
}
public class CustomPhoneStateListener extends PhoneStateListener
{
Context context; //Context to make Toast if required
ActivityManager activityManager;
public CustomPhoneStateListener(Context context)
{
super();
this.context = context;
}
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
Log.v("PhoneStateBroadcastReceiver", "onCallStateChanged state"+state);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
Toast.makeText(context, "=CALL_STATE_IDLE==", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Toast.makeText(context, "CALL_STATE_OFFHOOK", Toast.LENGTH_LONG).show();
break;
case TelephonyManager.CALL_STATE_RINGING:
Toast.makeText(context, "CALL_STATE_RINGING", Toast.LENGTH_LONG).show();
break;
default:
break;
}
}
}
}
I have seen some applications there are recording a voice when call was accepted. I want to know the state of accepting call.
Is there any other state or listener to know when the callee is answered the call?
The state will be OFF_HOOK
Device call state: Off-hook. At least one call exists that is dialing, active, or on hold, and no calls are ringing or waiting.
You can see on this link:
http://developer.android.com/reference/android/telephony/TelephonyManager.html
I want to create an android application that changes the mode to ringing from vibration when incoming call from particular number
For Changing ringing from vibration on incoming call use Use TelephonyManager,AudioManager and PhoneStateListener as:
TelephonyManager mTelephonyMgr = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
mTelephonyMgr.listen(new TeleListener(), PhoneStateListener.LISTEN_CALL_STATE);
class TeleListener extends PhoneStateListener
{
public void onCallStateChanged(int state, String incomingNumber)
{
super.onCallStateChanged(state, incomingNumber);
switch (state)
{
case TelephonyManager.CALL_STATE_IDLE:
//CALL_STATE_IDLE;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//CALL_STATE_OFFHOOK;
break;
case TelephonyManager.CALL_STATE_RINGING:
//CALL_STATE_RINGING
//CHECK YOUR PARTICULAR NUMBER HERE
if(incomingNumber=="1234567890")
{
// USE AudioManager for Settingringing from vibration
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
switch (am.getRingerMode()) {
case AudioManager.RINGER_MODE_NORMAL:
Log.i("MyApp","NORMAL mode");
am.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
}
}
else
{
//DO SOMETHING HERE
}
break;
default:
break;
}
}
}
add <uses-permission android:name="android.permission.READ_PHONE_STATE"> permission in manifest.xml
or How we Get Phone State using BroadcastReceiver see this tutorial:
Get Phone State When Someone is calling using BroadcastReceiver Example
I know this is a asked question. By using broadcast receiver and using
android.intent.action.PHONE_STATE
in receiver tag, you can know the actions of a phone. But how can I identify whether it is an incoming call or a outgoing call?
here is my code
#Override
public void onReceive(Context context, Intent intent)
{
this.context = context ;
System.out.println(":::called onReceiver:::");
TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneCallListener = new PhoneStateListener()
{
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
switch(state)
{
case TelephonyManager.CALL_STATE_RINGING:
isRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
if (isRinging)
{
hasAttended = true ;
isRinging = false;
}
break;
case TelephonyManager.CALL_STATE_IDLE:
if (hasAttended)
{
isCallEnded = true ;
hasAttended = false;
}
break;
}
if(isCallEnded)
{
isCallEnded=false;
Intent callIntent = new Intent(context.getApplicationContext(),MyActivity.class);
callIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(callIntent);
}
super.onCallStateChanged(state, incomingNumber);
}
};
here each time a phoneCallListener object is created and increases the calling rate of onCallStateChanged by one each time..
This is a handy tutorial that uses both broadcast receiver and using android.intent.action.PHONE_STATE that creates different toasts to appear depending on the calls state. When you get this working you will be able to manipulate the code to do what you want when a call is either incoming or outgoing
First your class must extend BroadcastReceiver
Next you have to create a method that will listen to the phones state... PhoneStateListener which will listen to when the Phone state changes
Then do a simple switch case to catch the call depending on if it is an incoming call or an outgoing call
EDIT
All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled/missed the call. If the current state is offhook and the previous state was ringing, they answered the call.
switch(state){
case TelephonyManager.CALL_STATE_RINGING:
Log.i(LOG_TAG, "RINGING");
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.i(LOG_TAG, "OFFHOOK");
if (!wasRinging) {
// Start your new activity
} else {
// Cancel your old activity
}
// this should be the last piece of code before the break
wasRinging = true;
break;
case TelephonyManager.CALL_STATE_IDLE:
Log.i(LOG_TAG, "IDLE");
// this should be the last piece of code before the break
wasRinging = true;
break;
}