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

F958fbf402a67a1742335a4894e041e5?s=128

magiepooh

June 23, 2016
Tweet

Transcript

  1. How to detect phone call @magiepooh

  2. About me @magiepooh @magie_pooh Takuma Fujita

  3. Company AbemaTV, Inc. (Cyber Agent, Inc.)

  4. How to detect phone call

  5. Pattern 1: Use TelephonyManager

  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. 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
  8. 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
  9. 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
  10. 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
  11. 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
  12. Need Permission… <uses-permission android:name= "android.permission.READ_PHONE_STATE"/>

  13. Pattern 2: Use AudioManager

  14. AudioManager. OnAudioFocusChangeListener • Interface definition for a callback to be

    invoked when the audio focus of the system is updated • Added in API level 8
  15. 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); } }
  16. 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); } }
  17. 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); } }
  18. 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); } }
  19. 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
  20. @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; } }
  21. @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; } }
  22. @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 } }
  23. Conclusion • TelephonyManager needs permission • AudioManager do not need

    permission • but status is complicated
  24. (๑•̀ŷ•́)و✧