2

我有以下代码,并且我已经阅读了当两方之间的电话连接并且对方实际上可以看到您正在呼叫并接听您的电话时,振铃状态处于活动状态。空闲状态是呼叫断开时,OFF_HOOK 状态是您按下按钮拨号的时刻。我进入 OFF_HOOK 和空闲状态但无法进入振铃状态。可能是什么问题?。任何帮助将不胜感激。

public void onReceive(final Context context, Intent intent) {
            TelephonyManager Tm=(TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);
            Tm.listen(new PhoneStateListener(){
                public void  onCallStateChanged(int state,String number) {
                    super.onCallStateChanged(state, number);
                    switch (state) {
                      case TelephonyManager.CALL_STATE_IDLE:
                          //when Idle i.e no call
                          Toast.makeText(context, "Phone state Idle", 1).show();

                          break;
                      case TelephonyManager.CALL_STATE_OFFHOOK:

                          //when Off hook i.e in call
                          //Make intent and start your service here
                          Toast.makeText(context, "Phone state Off hook", 1).show();

                          break;
                      case TelephonyManager.CALL_STATE_RINGING:
                          Toast.makeText(context, "Phone state Ringing", 1).show();
                        break;


                      default:
                          break;
                      }
                  }    

            },PhoneStateListener.LISTEN_CALL_STATE);
4

2 回答 2

3

TelephonyManager.CALL_STATE_RINGING用于来电。它不是为了拨出电话的对方振铃。

TelephonyManager.CALL_STATE_RINGING当您有来电并且您的电话正在响铃时将触发。

也供您参考。没有公开的api来检测对方的振铃状态以进行呼出。

于 2013-10-22T05:30:44.043 回答
0

也许试试我在网上某处复制的这个帮助类,不记得在哪里

/*Helper class to detect incoming and outgoing calls.*/
public class CallHelper {
    private Activity activity;
    private TelephonyManager tm;
    private CallStateListener callStateListener;
    private OutgoingReceiver outgoingReceiver;
    public String TelephoneNumber;
    public boolean InCall = false;

    public CallHelper(Activity activity) {
        this.activity = activity;
        callStateListener = new CallStateListener();
        outgoingReceiver = new OutgoingReceiver();
    }

    /*Listener to detect incoming calls.*/
    private class CallStateListener extends PhoneStateListener {

        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            switch (state) {
                case TelephonyManager.CALL_STATE_RINGING: {
                    TelephoneNumber = incomingNumber;
                    inout = "in";
                    InCall = false;
                    break;
                }
                case TelephonyManager.CALL_STATE_OFFHOOK: {
                    if (!InCall) {
                        InCall = true;
                        //Do your stuff here when in call...
                    }
                    break;
                }
                case TelephonyManager.CALL_STATE_IDLE: {
                    InCall = false;
                    break;
                }
                default: {
                    InCall = false;
                    break;
                }
            }
        }
    }

    /*Broadcast receiver to detect the outgoing calls.*/
    public class OutgoingReceiver extends BroadcastReceiver {
        public OutgoingReceiver() { }

        @Override
        public void onReceive(Context context, Intent intent) {
            TelephoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);
        }
    }

    /*Start calls detection.*/
    public void start() {
        tm = (TelephonyManager) activity.getSystemService(Activity.TELEPHONY_SERVICE);
        tm.listen(callStateListener, PhoneStateListener.LISTEN_CALL_STATE);

        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);
        activity.registerReceiver(outgoingReceiver, intentFilter);
    }

    /*Stop calls detection.*/
    public void stop() {
        tm.listen(callStateListener, PhoneStateListener.LISTEN_NONE);
        activity.unregisterReceiver(outgoingReceiver);
    }
}
于 2014-06-29T05:56:16.647 回答