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;
}
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.
In my android application, I wanna call a person from the application and drop the call if the call get connected successful ( after 2 rings or so ). I can call to the number via this but is there any way to drop the call and get the event after 2 rings or a single ring ?
My calling code looks like
String posted_by = "1234567890";
String uri = "tel:" + posted_by.trim() ;
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
I am new to android
Please help, Thanks in advance
You can make use of the following in a receiver:
/** The listener. */
private final PhoneStateListener listener = new PhoneStateListener() {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
Log.d("TAG", "IDLE");
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_NORMAL) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
} else if (audiomanager.getRingerMode() == AudioManager.RINGER_MODE_VIBRATE) {
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
Log.d("TAG", "OFFHOOK");
audiomanager.setRingerMode(AudioManager.RINGER_MODE_VIBRATE);
break;
case TelephonyManager.CALL_STATE_RINGING:
Log.d("TAG", "RINGING====>" + incomingNumber);
if (incomingNumber.equalsIgnoreCase("phoneNumber")) {
switch (audiomanager.getRingerMode()) {
case AudioManager.RINGER_MODE_VIBRATE:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
case AudioManager.RINGER_MODE_SILENT:
audiomanager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
audiomanager.setStreamVolume(AudioManager.STREAM_RING,volRing,0);
break;
}
}
break;
}
};
There's no way of doing this in Android. However you could use TelephonyManager and PhoneStateListener to check the phone states. When you got this information you should be able to implement the functionality you need.This post should help you: Android READ PHONE STATE?.
Good luck!
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 working on an app in which I am playing media player using service. The app is working fine but when i receive a call or a message, the music keeps on playing. My requirement is when I get a call, the music should be paused and on call disconnection, it should resume from where it has stopped. How can I achieve this?
I am not sure how you coded your app but this should help you.
Check out this How to know the moment when the called person picks up his phone You would just need to pause the music in the switch statement for the specified event
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
private PhoneStateListener mPhoneListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// do something...
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
// this state is the one you're looking for
break;
case TelephonyManager.CALL_STATE_IDLE:
// do something...
break;
default:
Log.d(TAG, "Unknown phone state=" + state);
}
} catch (RemoteException e) {}
}
};
EDIT: here is another link Detect if an outgoing call has been answered
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