Android check when outgoing call is received by callee

一世执手 提交于 2019-12-11 00:08:02

问题


I'm developing on android framework,

I want to fire an event when an outgoing call is received by the callee , and also when the call is ended (from any of the two sides)


回答1:


Inorder to know wheter the calling party has recieved the call, you will need to create a listener.

class PhoneInfo extends BroadcastReceiver {
/**
 * Getting the System Telephony Service and registering a listener for Voice Call state
 */
@Override
public void onReceive(Context context, Intent intent) {
    IncomingCallListener phoneListener = new IncomingCallListener();
    TelephonyManager telephony = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    telephony.listen(phoneListener, PhoneStateListener.LISTEN_CALL_STATE);
}

class IncomingCallListener extends PhoneStateListener {

    public void onCallStateChanged(int state, String incomingNumber) {
        Log.i(logcat,"CALL_STATE changed " + callflag);

        switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            Log.i(logcat,"CALL_STATE_IDLE");
                                    //This is where call ends.
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:
                                    //This is where we know call is established
            break;

        case TelephonyManager.CALL_STATE_RINGING:
            Log.i(logcat,"CALL_STATE_RINGING");
            break;
        }


    }

}

Register this with your activity as phoneInfo = new PhoneInfo(this); registerReceiver(phoneInfo, new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL));

Now with Logs u can see how states are changed when a call is dialed or received.



来源:https://stackoverflow.com/questions/12913267/android-check-when-outgoing-call-is-received-by-callee

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!