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. How to detect
    phone call
    @magiepooh

    View full-size slide

  2. About me
    @magiepooh
    @magie_pooh
    Takuma Fujita

    View full-size slide

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

    View full-size slide

  4. How to detect
    phone call

    View full-size slide

  5. Pattern 1:
    Use TelephonyManager

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  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

    View full-size slide

  12. Need Permission…
    "android.permission.READ_PHONE_STATE"/>

    View full-size slide

  13. Pattern 2:
    Use AudioManager

    View full-size slide

  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

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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);
    }
    }

    View full-size slide

  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

    View full-size slide

  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;
    }
    }

    View full-size slide

  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;
    }
    }

    View full-size slide

  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
    }
    }

    View full-size slide

  23. Conclusion
    • TelephonyManager needs permission
    • AudioManager do not need permission
    • but status is complicated

    View full-size slide

  24. (๑•̀ŷ•́)و✧

    View full-size slide