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!
Related
From this blog I learned that, for all media applications, I should request AudioFocus and start the playback only if the AudioFocus is granted.
am.requestAudioFocus(audioFocusChangeListener,AudioManager.STREAM_ALARM,AudioManager.AUDIOFOCUS_GAIN);
I am making an alarm application.
As an alarm, it should always ring until dismissed - except during phone calls (it is logical).
I can track if a call is ringing or idle by using the TelephonyManager
TelephonyManager telephonyManager =
(TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
switch (telephonyManager.getCallState()) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
break;
default:
outsidePause = true;
break;
}
PhoneStateListener phoneStateListener = new PhoneStateListener() {
#Override public void onCallStateChanged(int state, String incomingNumber) {
switch (state) {
case TelephonyManager.CALL_STATE_IDLE:
outsidePause = false;
Settings.System.putInt(getContentResolver(), Settings.System.SCREEN_BRIGHTNESS_MODE,
Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
android.provider.Settings.System.putInt(getContentResolver(),
android.provider.Settings.System.SCREEN_BRIGHTNESS, (0));
am.setStreamVolume(AudioManager.STREAM_ALARM, 0, 0);
setInnerData();
startAlarm();
break;
default:
outsidePause = true;
vibrator.cancel();
pauseAlarm();
break;
}
super.onCallStateChanged(state, incomingNumber);
}
};
Taking in account the TelephonyManager, is it useful and nessesary to use the AudioFocus (if it is, which mode is preferrable)?
Can another application take it from mine, making silencing the alarm?
Seems, that using Audio focus if ok. Othet application does not take it, untill I abandone it.
Hey guys you can use below code in your onAudioFocusChange() method to Manage your Audio Focus when Alarm or Music Player or WhatsApp video call or Phone call comes in all these scenario it will work
// (Below code) Mandatory method for onAudioFocusChange
#Override
public void onAudioFocusChange(int focusChange) {
switch(focusChange)
{
case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT:
// Whenever Alarm is started command comes to this place
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
say = false;
mediaPlayer.pause();
break;
case AudioManager.AUDIOFOCUS_GAIN :
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, pause);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(pause);
say = false;
mediaPlayer.start();
}
break;
case AudioManager.AUDIOFOCUS_LOSS:
if(mediaPlayer.isPlaying())
{
notificationView.setImageViewResource(R.id.remoteViewImageView, playbutton);
notificationManager.notify(1,notification);
playButtonImage.setImageResource(playbutton);
mediaPlayer.pause();
say = false;
audioManager.abandonAudioFocus(this);
}
break;
}
}
}
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 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;
}
I have audio recording, when a phone call come I need to stop the recording, how can I do this?
You have to use the PhoneStateListener:
TelephonyManager tm = (TelephonyManager)getSystemService(TELEPHONY_SERVICE);
tm.listen(mPhoneListener, PhoneStateListener.LISTEN_CALL_STATE);
// somewhere else
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:
// do something...
break;
case TelephonyManager.CALL_STATE_IDLE:
// do something...
break;
default:
Log.d(TAG, "Unknown phone state=" + state);
}
} catch (RemoteException e) {}
}
};
Make sure to include this permission in your Manifest:
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>
I've never tried to access the phone stuff on Android but check here -
http://developer.android.com/reference/android/telephony/package-summary.html
and here -
http://developer.android.com/reference/android/telephony/PhoneStateListener.html
and here -
http://developer.android.com/reference/android/telephony/TelephonyManager.html