且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检测,当电话接通或拒绝

更新时间:2022-10-15 14:57:58

在你的onReceive:

  PhoneStateChangeListener pscl =新PhoneStateChangeListener;
TelephonyManager TM =(TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.li​​sten(pscl,PhoneStateListener.LISTEN_CALL_STATE);
 

单独的类:

 私有类PhoneStateChangeListener扩展PhoneStateListener {
    公共静态布尔wasRinging;
    @覆盖
    公共无效onCallStateChanged(INT状态,串incomingNumber){
        开关(州){
            案例TelephonyManager.CALL_STATE_RINGING:
                 Log.i(LOG_TAG,振铃);
                 wasRinging = TRUE;
                 打破;
            案例TelephonyManager.CALL_STATE_OFFHOOK:
                 Log.i(LOG_TAG,摘机);

                 如果(!wasRinging){
                     //启动新活动
                 } 其他 {
                     //取消旧的活动
                 }

                 //这应该是最后一块code在中场休息前
                 wasRinging = TRUE;
                 打破;
            案例TelephonyManager.CALL_STATE_IDLE:
                 Log.i(LOG_TAG空转);
                 //这应该是最后一块code在中场休息前
                 wasRinging = FALSE;
                 打破;
        }
    }
}
 

所有你需要做的是写一些code,以检查previous状态是'响'。 如果当前状态为空闲状态和previous状态里响起,他们取消了电话。 如果当前状态是摘机和previous状态里响起,他们响应号召。

I managed to prepare an activity when the phone is ringing. Now I need to know how to cancel this activity when I anwser the phone or I reject the call.Do I call EXTRA_STATE_IDLE?EXTRA_STATE_OFFHOOK?

Any ideas?

<receiver android:name=".IncomingBroadcastReceiver">                     
        <intent-filter>                                                   
            <action android:name="android.intent.action.PHONE_STATE"/>   
        </intent-filter>
    </receiver> 



public class IncomingBroadcastReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {
    String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);
    // If an incoming call arrives
    if (state.equals(TelephonyManager.EXTRA_STATE_RINGING))
    { //Did my work }

in your onReceive:

PhoneStateChangeListener pscl = new PhoneStateChangeListener;
TelephonyManager tm = (TelephonyManager)this.getSystemService(Context.TELEPHONY_SERVICE);
tm.listen(pscl, PhoneStateListener.LISTEN_CALL_STATE);

separate class:

private class PhoneStateChangeListener extends PhoneStateListener {
    public static boolean wasRinging;
    @Override
    public void onCallStateChanged(int state, String incomingNumber) {
        switch(state){
            case TelephonyManager.CALL_STATE_RINGING:
                 Log.i(LOG_TAG, "RINGING");
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                 Log.i(LOG_TAG, "OFFHOOK");

                 if (!wasRinging) {
                     // Start your new activity
                 } else {
                     // Cancel your old activity
                 }

                 // this should be the last piece of code before the break
                 wasRinging = true;
                 break;
            case TelephonyManager.CALL_STATE_IDLE:
                 Log.i(LOG_TAG, "IDLE");
                 // this should be the last piece of code before the break
                 wasRinging = false;
                 break;
        }
    }
}

All you need to do is write some code to check if the previous state was 'ringing'. If the current state is idle and the previous state was ringing, they cancelled the call. If the current state is offhook and the previous state was ringing, they answered the call.