2

我正在开发android框架,

我想在被叫方收到拨出电话时以及通话结束时(从两侧)触发一个事件

4

1 回答 1

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

现在通过日志,您可以看到在拨打或接听电话时状态是如何变化的。

于 2012-10-16T11:07:03.260 回答