11

I am wondering if there is a way I can detect a skype call/telegram/whatsapp/fb messenger call etc in progress or incoming call etc just like the regular phone call with telephony manager/listener? I would like to have some kind of mechanism that detects an ongoing /incoming etc call from skype/telegram etc for my app. I came across this solution :Call Detection for skype in android but not sure if it'll work for all generic messenger apps. Is there a hack or any kind of listener I can implement on my side that allows me to detect these? Any ideas would be awesome.

Thanks!

  • does anyone have any clue about this? or is this simply not possible?? –  Mar 19 '18 at 18:06

1 Answers1

16

You can use the AudioManager system service and check for audio mode using AudioManager#getMode(). According to the docs, AudioManager#getMode() returns the current audio mode which can be one of following:

  • MODE_NORMAL (0x00000000): Normal audio mode: not ringing and no call established.

  • MODE_RINGTONE (0x00000001): Ringing audio mode. An incoming is being signaled.

  • MODE_IN_CALL (0x00000002): In call audio mode. A telephony call is established.

  • MODE_IN_COMMUNICATION (0x00000003): In communication audio mode. An audio/video chat or VoIP call is established (added in API 11).

Using AudioManager#getMode() you can check if any other app is currently holding an audio focus of type MODE_IN_COMMUNICATION and handle your call accordingly.

AudioManager am = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE);
final int mode = am.getMode();
if(AudioManager.MODE_IN_CALL == mode){
  // device is in a telephony call
} else if(AudioManager.MODE_IN_COMMUNICATION == mode){
  // device is in communiation mode, i.e. in a VoIP or video call
} else if(AudioManager.MODE_RINGTONE == mode){
  // device is in ringing mode, some incoming is being signalled
} else {
  // device is in normal mode, no incoming and no audio being played
}

P.S. I've tested this with whatsapp messenger and it works! Cannot say the same for other messenger apps.

riyaz-ali
  • 8,677
  • 2
  • 22
  • 35
  • I do check that onStop as whatsapp takes over and my app is thrown in the background.However, it doesn't work with telegram as telegram doesn't even invoke the onpause state.Is there a way I can detect an incoming activity or notification for call? –  Mar 23 '18 at 11:29
  • From what you have stated in the OP I assume you were talking about the case where your app is not active and then someone rings the user which invokes some kind of a Service (or Activity maybe?) in your app. Here you can check if the device is already in a call and return the status to the caller. Am I right here? – riyaz-ali Mar 23 '18 at 11:34
  • 1
    From what you stated in the comment it seems like your app is already in a call in which case you should be holding an AudioFocus any way... – riyaz-ali Mar 23 '18 at 11:35
  • That's true.If my app is already in a call, should I relinquish my audio focus when another voip call comes in? or is that not recommended? –  Mar 23 '18 at 12:56
  • I don't think so... Imagine being in a conversation and suddenly the app you are using hands the audio focus to some other app! how inconvienient would that be as a user! besides that your app is on the _top_ priority now as the user is actively using it and any _surprises_ at this point is very bad from ux point. – riyaz-ali Mar 24 '18 at 02:55
  • So in short you shouldn't abandon the audio focus if you are already in a call!! I guess _most well behaving_ apps on the system will co-operate and handle the case accordingly! Also you should handle the case similarly and not request audio focus in your app if the user is already on a call in another app... – riyaz-ali Mar 24 '18 at 02:58
  • you are right. I guess the best behavior in this case would be to not do anything I guess.good answer though, you get the points! –  Mar 24 '18 at 12:52
  • How can it be defined in a service so that always I can get notified for changes? thanks. – Bilal Şimşek Oct 22 '21 at 18:25