Android 来去电监听,电话挂断(1)

public void onServiceStateChanged(ServiceState serviceState) {

super.onServiceStateChanged(serviceState);

Log.d(PhoneListenService.TAG, "CustomPhoneStateListener onServiceStateChanged: " + serviceState);

}

@Override

public void onCallStateChanged(int state, String incomingNumber) {

Log.d(PhoneListenService.TAG, "CustomPhoneStateListener state: "

  • state + " incomingNumber: " + incomingNumber);

switch (state) {

case TelephonyManager.CALL_STATE_IDLE: // 电话挂断

break;

case TelephonyManager.CALL_STATE_RINGING: // 电话响铃

Log.d(PhoneListenService.TAG, “CustomPhoneStateListener onCallStateChanged endCall”);

HangUpTelephonyUtil.endCall(mContext);

break;

case TelephonyManager.CALL_STATE_OFFHOOK: // 来电接通 或者 去电 但是没法区分

break;

}

}

}

三种状态源码解释

/** Device call state: No activity. */

public static final int CALL_STATE_IDLE = 0; // 电话挂断

/** Device call state: Ringing. A new call arrived and is

  • ringing or waiting. In the latter case, another call is

  • already active. */

public static final int CALL_STATE_RINGING = 1; // 来电响铃

/** Device call state: Off-hook. At least one call exists

  • that is dialing, active, or on hold, and no calls are ringing

  • or waiting. */

public static final int CALL_STATE_OFFHOOK = 2; // 来电接通 或者 去电拨号 但是没法区分出来

2.2 来去电监听方式二(广播监听,但是时机比上面的 PhoneStateListener 方式要晚一点)

<receiver android:name=“.PhoneStateReceiver”

android:enabled=“true”

android:process=“:PhoneListenService”>

public class PhoneStateReceiver extends BroadcastReceiver {

@Override

public void onReceive(Context context, Intent intent) {

if (intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)) {

// 去电,可以用定时挂断

} else {

//来电

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE);

Log.d(PhoneListenService.TAG, "PhoneStateReceiver onReceive state: " + state);

if (state.equalsIgnoreCase(TelephonyManager.EXTRA_STATE_RINGING)) {

Log.d(PhoneListenService.TAG, “PhoneStateReceiver onReceive endCall”);

HangUpTelephonyUtil.endCall(context);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值