android中的Get call receive事件

android中的Get call receive事件,android,phone-call,Android,Phone Call,我可以通过编码在android中获取来电接听和结束通话事件吗?我想知道,当我接到来电时,应该显示祝酒词。和来电一样,我希望在结束通话时,在我的应用程序中显示toast消息。请建议我。您需要使用广播接收器。试试这个。你需要使用广播接收器。请尝试此操作。如果希望仅在应用程序的一个活动中使用此操作,请使用以下代码: PhoneStateListener phoneStateListener = new PhoneStateListener() { @Override p

我可以通过编码在android中获取来电接听和结束通话事件吗?我想知道,当我接到来电时,应该显示祝酒词。和来电一样,我希望在结束通话时,在我的应用程序中显示toast消息。请建议我。

您需要使用广播接收器。试试这个。

你需要使用广播接收器。请尝试此操作。

如果希望仅在应用程序的一个活动中使用此操作,请使用以下代码:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {
                // Incoming call: Pause music
                Log.i("Phone", "Ringing");

            } else if (state == TelephonyManager.CALL_STATE_IDLE) {


                Log.i("Phone", "Idle");
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                // A call is dialing, active or on hold
                Log.i("Phone", "offhook");

            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
或者,如果要在应用程序外部显示,请尝试使用
服务

有一些很好的服务教程可用

首先阅读开发者


同时也要注意这一点。

如果只希望在应用程序的一个活动中使用它,请使用以下代码:

PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {

            if (state == TelephonyManager.CALL_STATE_RINGING) {
                // Incoming call: Pause music
                Log.i("Phone", "Ringing");

            } else if (state == TelephonyManager.CALL_STATE_IDLE) {


                Log.i("Phone", "Idle");
            } else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {
                // A call is dialing, active or on hold
                Log.i("Phone", "offhook");

            }
            super.onCallStateChanged(state, incomingNumber);
        }
    };
或者,如果要在应用程序外部显示,请尝试使用
服务

有一些很好的服务教程可用

首先阅读开发者

同时也调查一下这个