当其他方接听电话并关闭电话时执行某些操作

时间:2013-12-25 14:12:06

标签: java android call

我使用此代码调用:

Uri callUri = Uri.parse("tel://0000");
Intent callIntent = new Intent(Intent.ACTION_CALL,callUri);
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_NO_USER_ACTION);
startActivity(callIntent);

我想在以下时间做点什么:

  • 对方接听电话
  • 其他方关闭通话
  • 用户关闭通话
  • 用户在对方接听电话之前关闭电话

以及如何关闭通话

先谢谢

1 个答案:

答案 0 :(得分:0)

您应该使用 TelephonyManager 来处理呼叫。看看下面的课......

private class PhoneCallListener extends PhoneStateListener {

        private boolean phoneInACall = false;

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (TelephonyManager.CALL_STATE_RINGING == state) {
                // THE PHONE IS CURRENTLY RINGING!
            }

            if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
                // THE PHONE IS ACTIVE, DIALING OR ON HOLD!
                phoneInACall = true;
            }
            if (TelephonyManager.CALL_STATE_IDLE == state) {
                // IDLE - NO ACTIVITY. IF PHONEINACALL WAS TRUE, IT MEANS THE CALL ENDED
                if (phoneInACall) {
                    // RESTART YOUR MAIN ACTIVITY BECAUSE A CALL ENDED!
                    phoneCalling = false;
                }

            }
        }
    }

您可以在 onCreate 方法中使用此类,方法是创建此 PhoneStateListener 的实例并获取 TelephonyManager 的实例来调用监听方法,以收听指定电话状态变化的通知......

PhoneCallListener phoneCallListener = new PhoneCallListener();
TelephonyManager telManager = (TelephonyManager) this.getSystemService(Context.TELEPHONY_SERVICE);
telManager.listen(phoneCallListener, PhoneStateListener.LISTEN_CALL_STATE);

此代码可以帮助您解决问题:)祝您好运!