Handling Android application pause on incoming call and resume after call end

前端 未结 2 602
无人共我
无人共我 2020-12-30 16:41

I want to pause my android application when the phone receives an incoming call. After the call ends, I want my applications to resume automatically.

How would this

2条回答
  •  轮回少年
    2020-12-30 17:39

    private class EndCallListener extends PhoneStateListener {
      private boolean active = false;
      @Override
      public void onCallStateChanged(int state, String incomingNumber) {
        if(TelephonyManager.CALL_STATE_RINGING == state) {
          Log.i("EndCallListener", "RINGING, number: " + incomingNumber);
        }
        if(TelephonyManager.CALL_STATE_OFFHOOK == state) {
          //wait for phone to go offhook (probably set a boolean flag) so you know your app initiated the call.
          active = true;
          Log.i("EndCallListener", "OFFHOOK");
        }
        if(TelephonyManager.CALL_STATE_IDLE == state) {
          //when this state occurs, and your flag is set, restart your app
          Log.i("EndCallListener", "IDLE");
          if (active) {
            active = false;
            // stop listening                   
            TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService( Context.TELEPHONY_SERVICE );
            mTM.listen(this, PhoneStateListener.LISTEN_NONE);
            // restart the inbox activity
            //Intent intent = new Intent(m_activity, MDInboxActivity.class);
            //m_activity.startActivity(intent);
          }
        }
      }
    }
    

    And you can initialize the above class by calling the below lines:

    try {
      EndCallListener callListener = new EndCallListener();
      TelephonyManager mTM = (TelephonyManager) m_activity.getSystemService(Context.TELEPHONY_SERVICE);
      mTM.listen(callListener, PhoneStateListener.LISTEN_CALL_STATE);
    } catch(Exception e) {
      Log.e("callMonitor", "Exception: "+e.toString());
    }
    

提交回复
热议问题