Upgrade to Pro — share decks privately, control downloads, hide ads and more …

How to detect phone call

How to detect phone call

How to detect phone call for Android

magiepooh

June 23, 2016
Tweet

More Decks by magiepooh

Other Decks in Technology

Transcript

  1. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  2. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  3. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  4. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  5. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  6. PhoneStateChangeListener l = new PhoneStateChangeListener(); TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVI CE);

    tm.listen(l, PhoneStateListener.LISTEN_CALL_STATE); public class MyListener extends PhoneStateListener { @Override public void onCallStateChanged(int state, String incomingNumber) { switch(state){ case TelephonyManager.CALL_STATE_RINGING: //… case TelephonyManager.CALL_STATE_OFFHOOK: //… case TelephonyManager.CALL_STATE_IDLE: //… } } } http://stackoverflow.com/questions/9684866/how-to-detect-when-phone-is-answered-or-rejected
  7. AudioManager. OnAudioFocusChangeListener • Interface definition for a callback to be

    invoked when the audio focus of the system is updated • Added in API level 8
  8. public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager;

    public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }
  9. public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager;

    public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }
  10. public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager;

    public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }
  11. public class MyAudioFocusMonitor implements AudioManager.OnAudioFocusChangeListener { private final AudioManager audioManager;

    public MyAudioFocusMonitor(Context context) { audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); } public void start() { audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, // Request permanent focus. AudioManager.AUDIOFOCUS_GAIN); } public void stop() { audioManager.abandonAudioFocus(this); } }
  12. Audio Focus • AUDIOFOCUS_GAIN • unknown duration • AUDIOFOCUS_GAIN_TRANSIENT •

    a short amount of time • AUDIOFOCUS_GAIN_TRANSIENT_EXCLUSIVE • ≒AUDIOFOCUS_GAIN_TRANSIENT • a short amount of time, during which no other applications, or system components, should play anything. • AUDIOFOCUS_GAIN_TRANSIENT_MAY_DUCK • ≒AUDIOFOCUS_GAIN_TRANSIENT • other audio applications to keep playing after having lowered their output level
  13. @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case

    AudioManager.AUDIOFOCUS_REQUEST_GRANTED: // play break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // pause break; case AudioManager.AUDIOFOCUS_LOSS: // stop audioManager.abandonAudioFocus(this); break; default: break; } }
  14. @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case

    AudioManager.AUDIOFOCUS_REQUEST_GRANTED: // … (play break; case AudioManager.AUDIOFOCUS_LOSS_TRANSIENT: // … (pause break; case AudioManager.AUDIOFOCUS_LOSS: // … (stop audioManager.abandonAudioFocus(this); break; } }
  15. @Override public void onAudioFocusChange(int focusChange) { switch (focusChange) { case

    AudioManager. AUDIOFOCUS_LOSS_TRANSIENT_CAN_DUCK: // Lower the volume case AudioManager. AUDIOFOCUS_GAIN: // Raise it back to normal } }