I have my application that uses the MediaPlayer class, I need that when a call comes in the sound is mute, but when the app goes to the background (when you press the home button for example) follow the playback of the audio.
public static class ReceptorLlamadas extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
call(context);
}
private void call(Context context) {
PhoneCallListener phoneListener = new PhoneCallListener();
TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private class PhoneCallListener extends PhoneStateListener {
public boolean isPhoneCalling = false;
Boolean wasRinging = false;
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (TelephonyManager.CALL_STATE_RINGING == state) {
// phone ringing
//Aquí ya detectas que el teléfono esta recibiendo una llamada entrante
}
if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
// active
isPhoneCalling = true;
if (mPlayer != null && mPlayer.isPlaying()) {
mPlayer.setVolume(0,0);
}
}
if (TelephonyManager.CALL_STATE_IDLE == state) {
isPhoneCalling = false;
}
}
}
and this I added to the manifest.xml
<receiver android:name=".MainActivity$ReceptorLlamadas">
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" />
</intent-filter>
</receiver>