[ACCEPTED]-Stopping & starting music on incoming calls-incoming-call
There are a few things you can do:
First 10 of all, you can listen for changes in the 9 call state using a PhoneStateListener
.
You can register the 8 listener in the TelephonyManager:
PhoneStateListener phoneStateListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
if (state == TelephonyManager.CALL_STATE_RINGING) {
//Incoming call: Pause music
} else if(state == TelephonyManager.CALL_STATE_IDLE) {
//Not in call: Play music
} else if(state == TelephonyManager.CALL_STATE_OFFHOOK) {
//A call is dialing, active or on hold
}
super.onCallStateChanged(state, incomingNumber);
}
};
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);
}
Remember 7 to unregister the listener when it's no 6 longer needed using the PhoneStateListener.LISTEN_NONE
:
TelephonyManager mgr = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
if(mgr != null) {
mgr.listen(phoneStateListener, PhoneStateListener.LISTEN_NONE);
}
For more information read the documentation.
Another thing 5 you can do is listening for the broadcast 4 android.intent.action.PHONE_STATE
. It will contain the extra TelephonyManager.EXTRA_STATE
which will 3 give you information about the call. Take a look at the documentation here.
Please 2 note that you'll need the android.permission.READ_PHONE_STATE
-permission in 1 both cases.
I think that AudioManager is the best and 2 fast solution. Here there is my implementation 1 example:
public class MyActivity extends Activity implements OnAudioFocusChangeListener {
private AudioManager mAudioManager;
@Override
public void onCreate(Bundle savedInstanceState) {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mAudioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
@Override
public void onDestroy(){
super.onDestroy();
...
mAudioManager.abandonAudioFocus(this);
...
}
@Override
public void onAudioFocusChange(int focusChange) {
if(focusChange<=0) {
//LOSS -> PAUSE
} else {
//GAIN -> PLAY
}
}
}
I hope it's helpful for you :-)
I think requestAudioFocus() should be able to handle this case 14 automatically. You don't need to check call 13 state explicitly.
Audio Focus is cooperative 12 in nature. That is, applications are expected 11 (and highly encouraged) to comply with the 10 audio focus guidelines, but the rules are 9 not enforced by the system. If an application 8 wants to play loud music even after losing 7 audio focus, nothing in the system will 6 prevent that. However, the user is more 5 likely to have a bad experience and will 4 be more likely to uninstall the misbehaving 3 application.
To request audio focus, you 2 must call requestAudioFocus() from the AudioManager, as 1 the example below demonstrates:
AudioManager audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
int result = audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN);
if (result != AudioManager.AUDIOFOCUS_REQUEST_GRANTED) {
// could not get audio focus.
}
You can try a broadcast receiver.
Create 2 a class CallReceiver
which extends BroadcastReceiver
.
package com.example.callreceiver;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
public class CallReceiver extends BroadcastReceiver{
TelephonyManager telManager;
Context context;
@Override
public void onReceive(Context context, Intent intent) {
this.context=context;
telManager = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}
private final PhoneStateListener phoneListener = new PhoneStateListener() {
@Override
public void onCallStateChanged(int state, String incomingNumber) {
try {
switch (state) {
case TelephonyManager.CALL_STATE_RINGING: {
//PAUSE
break;
}
case TelephonyManager.CALL_STATE_OFFHOOK: {
break;
}
case TelephonyManager.CALL_STATE_IDLE: {
//PLAY
break;
}
default: { }
}
} catch (Exception ex) {
}
}
};
}
Then Add this line 1 in manifest.xml file to register it on the App
<receiver android:name="CallReceiver" >
<intent-filter>
<action android:name="android.intent.action.PHONE_STATE" >
</action>
</intent-filter>
</receiver>
For me idle state was coming while there 2 was incoming call, the quick fix is to check 1 in the broadcast receiver
BroadcastReceiver phonestatereceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Bundle extras = intent.getExtras();
if (extras != null) {
String state = extras.getString(TelephonyManager.EXTRA_STATE);
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
//pause here
}
else if (state.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
//pause here
}
else if (state.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
//play here
}
}
}
};
Based on mickesource Updated for android Oreo+ and 3 kotlin . I added in service , you can follow 2 same for activity or fragment .
class MusicService : Service(), AudioManager.OnAudioFocusChangeListener {
var audioManager: AudioManager? = null
var audioFocusRequest:AudioFocusRequest?=null
override fun onCreate() {
super.onCreate()
setAudioFocusChangeListener()
}
private fun setAudioFocusChangeListener() {
audioManager = getSystemService(AUDIO_SERVICE) as AudioManager?
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
audioFocusRequest=AudioFocusRequest.Builder(AudioManager.AUDIOFOCUS_GAIN)
.setAudioAttributes(
AudioAttributes.Builder()
.setUsage(AudioAttributes.USAGE_MEDIA)
.setContentType(AudioAttributes.CONTENT_TYPE_MUSIC)
.build()
)
.setAcceptsDelayedFocusGain(true)
.setOnAudioFocusChangeListener(this).build()
audioManager?.requestAudioFocus(audioFocusRequest!!)
} else {
@Suppress("deprecation")
audioManager?.requestAudioFocus(
this,
AudioManager.STREAM_MUSIC,
AudioManager.AUDIOFOCUS_GAIN
)
}
}
override fun onAudioFocusChange(focusChange: Int) {
if(focusChange<=0){
//Pause audio
}else{
//Play audio
}
}
Tested on 1 Android 10 for regular and what's app call
More Related questions
We use cookies to improve the performance of the site. By staying on our site, you agree to the terms of use of cookies.