AudioManager is unreliable - android

AudioManager is unreliable in onCallStateChanged. During a phone call I need it to turn on speaker phone and set the volume to max. It sometimes turns on speakerphone (usually during the second or later call) and rarely turns the volume up. My PhoneCallListener class is within my MainActivity class.
private class PhoneCallListener extends PhoneStateListener
{
private boolean isPhoneCalling = false;
#Override
public void onCallStateChanged(int state, String incomingNumber)
{
AudioManager aM = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
aM.setMode(AudioManager.MODE_IN_CALL);
aM.setSpeakerphoneOn(true);
if(TelephonyManager.CALL_STATE_RINGING == state)
{
//phone ringing
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, AudioManager.FLAG_SHOW_UI);
}
if(TelephonyManager.CALL_STATE_OFFHOOK == state)
{
//phone active
aM.setSpeakerphoneOn(true);
aM.adjustStreamVolume(AudioManager.STREAM_MUSIC, AudioManager.ADJUST_RAISE, 0);
isPhoneCalling = true;
}
if(TelephonyManager.CALL_STATE_IDLE == state)
{
aM.setSpeakerphoneOn(false);
if(isPhoneCalling)
{
Intent i = getBaseContext().getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
isPhoneCalling = false;
}
}
}
}
Within CALL_STATE_OFFHOOK I had to turn off AudioManager.FLAG_SHOW_UI because it would continually show the volume UI. Also, setting aM.setStreamVolume(AudioManager.STREAM_MUSIC, aM.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0); crashes the app for some reason.
Any suggestions on how to make AudioManager work every time so that speakerphone is on and volume is max during a phone call?
Edit: Even with setting speackphoneon to true as soon as the onCallStateChanged method is called, it still is not reliably turning the speakerphone on. The volume is also unreliable and can't seem to set it to max without it crashing.

Below is the code to do this. I have tested in a phone running lollipop. Write your PhoneStateListener as:
private class myPhoneStateListener extends PhoneStateListener {
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_OFFHOOK: //Call is established
Log.d("s#urav", "Call is Offhook now!");
try {
Thread.sleep(500); //We never know when the call is actually OffHook
} catch (InterruptedException e) {
Log.d("s#urav","Exception is:"+e);
}
audioManager.setSpeakerphoneOn(true);
break;
case TelephonyManager.CALL_STATE_IDLE: //Call is finished
//Maintain a flag and do this only if speakerphone has been set on OFFHOOK
/*audioManager.setMode(AudioManager.MODE_NORMAL);
audioManager.setSpeakerphoneOn(false);*/
break;
}
}
}
For raising the volume of the call you have to increase the volume of STREAM_VOICE_CALL. This code + increasing the volume of call stream will meet you requirements.

Related

Which AudioFocus do I need to set for an alarm application

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;
}
}
}

How to play sound when headphone connected in android programatically?

I am working on one of the project which need to play sound simultaneously when headphone is connected.
I am using below code but no luck
AudioManager am = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
am.setMode(AudioManager.MODE_NORMAL);
am.setSpeakerphoneOn(true);
You need to use a BroadcastReceiver to handle the action sent when the headset is plugged. You need after that to check if the action sent by the broadcast equals Intent.ACTION_HEADSET_PLUG in the onReceive method, then you can play your sound using MediaPlayer
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(Intent.ACTION_HEADSET_PLUG)) {
int state = intent.getIntExtra("state", -1);
switch (state) {
case 0:
//Headset is unplugged
break;
case 1:
//Headset is plugged
MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.song);
mediaPlayer.start();
break;
default:
Log.d(TAG, "I have no idea what the headset state is");
}
}
}
Please see this answer https://stackoverflow.com/a/13610712/2354845

Phone state listener always executes code

I have set phoneStateListener in my app which turns the phone silent when the phone is turned upside down during an incoming call. The problem is that the phone gets silent irrespective of any condition. Here is my code-
AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
TelephonyManager telephonyManager;
PhoneStateListener phoneStateListener;
telephonyManager = (TelephonyManager) this
.getSystemService(Context.TELEPHONY_SERVICE);
phoneStateListener = new PhoneStateListener() {
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
// callStateStr = "ringing. Incoming number is: "
// + incomingNumber;
if (z1 > -10 && z1 < -9) {//this is to detect if the phone is turned upside down or not. consider this defined.
am.setStreamVolume(AudioManager.STREAM_RING, 0,
AudioManager.FLAG_ALLOW_RINGER_MODES);
Log.d("abc", "turning silent");
Toast.makeText(getApplicationContext(), "turning silent",Toast.LENGTH_SHORT).show();
}
break;
}
}
};
During an incoming call, the code under if condition is executed whether i turn my phone or not.
What am I doing wrong? Is there any other method to achieve this purpose?

Pausing a service in background in android

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

How to make missed calls?

I am working on an Android app on which I want to be
able to make calls but with a very precise restriction, this is
"making missed calls". What I want is, to be able to hang up just the
moment the phone starts ringing.
right now I am able to know when the phone starts to try and make the
call, but for a few seconds there is no "ringing" activity over the
network, which is what I am willing to do.
How can I stop this exact moment?
Using the onCallStateChanged() via the PhoneStateListener you can only detect when the phone starts to make an outgoing call and when the outgoing call is hunged up but you can't determine when a "ringing" is started. I tried once, check out the code below:
An outgoing call starts from IDLE to OFFHOOK when dialed out, to IDLE when hunged up.
The only workaround is to use a timer to hang up after the outgoing call starts and some few seconds passes, but then, you're never guaranteed the phone will start ringing.
Here's the code:
public abstract class PhoneCallReceiver extends BroadcastReceiver {
static CallStartEndDetector listener;
#Override
public void onReceive(Context context, Intent intent) {
savedContext = context;
if(listener == null){
listener = new CallStartEndDetector();
}
TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telephony.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
}
public class CallStartEndDetector extends PhoneStateListener {
int lastState = TelephonyManager.CALL_STATE_IDLE;
boolean isIncoming;
public PhonecallStartEndDetector() {}
//Incoming call- IDLE to RINGING when it rings, to OFFHOOK when it's answered, to IDLE when hung up
//Outgoing call- from IDLE to OFFHOOK when dialed out, to IDLE when hunged up
#Override
public void onCallStateChanged(int state, String incomingNumber) {
super.onCallStateChanged(state, incomingNumber);
if(lastState == state){
//No change
return;
}
switch (state) {
case TelephonyManager.CALL_STATE_RINGING:
isIncoming = true;
//incoming call started
break;
case TelephonyManager.CALL_STATE_OFFHOOK:
//Transition of ringing->offhook are pickups of incoming calls. Nothing down on them
if(lastState != TelephonyManager.CALL_STATE_RINGING){
isIncoming = false;
//outgoing call started
}
break;
case TelephonyManager.CALL_STATE_IDLE:
//End of call(Idle). The type depends on the previous state(s)
if(lastState == TelephonyManager.CALL_STATE_RINGING){
// missed call
}
else if(isIncoming){
//incoming call ended
}
else{
//outgoing call ended
}
break;
}
lastState = state;
}
}
}
Use onCallStateChanged() in PhoneStateListener

Categories

Resources