当被叫方收到拨出电话时,Android检查

时间:2012-10-16 11:02:56

标签: android phone-call

我正在开发android框架,

我想在被叫方收到拨出电话时以及通话结束时(来自任何一方)发起事件

1 个答案:

答案 0 :(得分:0)

为了知道主叫方收到了电话,你需要创建一个听众。

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


    }

}

将此活动注册为您的活动 phoneInfo = new PhoneInfo(this); registerReceiver(phoneInfo, new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL));

现在有了日志,你可以看到拨打或接听电话时状态是如何改变的。