振铃状态呼出

时间:2013-10-22 04:58:20

标签: android telephonymanager

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

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

2 个答案:

答案 0 :(得分:3)

TelephonyManager.CALL_STATE_RINGING适用于来电。它不是为了打电话给另一方的响铃。

当有来电且手机响铃时,

TelephonyManager.CALL_STATE_RINGING会被触发。

还有您的信息。没有公共API可以检测出局呼叫的另一方的振铃状态。

答案 1 :(得分: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);
    }
}