Android来电监听和去电监听

我觉得写文章就得写得有用一些的,必须要有自己的思想,关于来电去电监听将按照下面三个问题展开

1、监听来电去电有什么用?

2、怎么监听,来电去电监听方式一样吗?

3、实战,有什么需要特别注意地方?


 

监听来电去电能干什么

1、能够对监听到的电话做个标识,告诉用户这个电话是诈骗、推销、广告什么的

2、能够针对那些特殊的电话进行自动挂断,避免打扰到用户

 

来电去电的监听方式(不一样的方式)

1、来电监听(PhoneStateListener)

  来电监听是使用PhoneStateListener类,使用方式是,将PhoneStateListener对象(一般是自己继承PhoneStateListener类完成一些封装)注册到系统电话管理服务中去(TelephonyManager

  然后通过PhoneStateListener的回调方法onCallStateChanged(int state, String incomingNumber) 实现来电的监听 (详细实现可以参考后面给出的拓展阅读部分)

  注册监听

复制代码
// phoneServiceName是服务名,一般是 "phone" --> Context.TELEPHONY_SERVICE
TelephonyManager telephonyManager = (TelephonyManager) mContext.getSystemService(phoneServiceName);
if(telephonyManager != null) {
    try {
        // 注册来电监听
        telephonyManager.listen(mTelephonyListener, PhoneStateListener.LISTEN_CALL_STATE);
    } catch(Exception e) {
        // 异常捕捉
    }
}
复制代码

  PhoneStateListener的onCallStateChanged方法监听来电状态

复制代码
@Override
public void onCallStateChanged(int state, String incomingNumber) {
    switch (state) {
        case TelephonyManager.CALL_STATE_IDLE:
            // 电话挂断
            break;
        case TelephonyManager.CALL_STATE_OFFHOOK:
            // 来电响铃
            break;
        case TelephonyManager.CALL_STATE_RINGING:
            // 来电接通
            break;
        default:
            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、去电监听(通过广播来实现)

复制代码
// OutgoingCallListener继承一个BroadcastReceiver
<receiver android:name="com.test.OutgoingCallListener" >
    <intent-filter>
        <action android:name="android.intent.action.PHONE_STATE"/>
        <action android:name="android.intent.action.NEW_OUTGOING_CALL" />
    </intent-filter>
</receiver>
复制代码

 

实战,有什么需要特别注意地方

1、双卡双待的手机怎么获取

  对于双卡手机,每张卡都对应一个Service和一个PhoneStateListener,需要给每个服务注册自己的PhoneStateListener,服务的名称还会有点变化,厂商可能会修改

复制代码
public ArrayList<String> getMultSimCardInfo() {
    // 获取双卡的信息,这个也是经验尝试出来的,不知道其他厂商有什么坑
    ArrayList<String> phoneServerList = new ArrayList<String>();
    for(int i = 1; i < 3; i++) {
        try {
            String phoneServiceName;
            if (MiuiUtils.isMiuiV6()) {
                phoneServiceName = "phone." + String.valueOf(i-1);
            } else {
                phoneServiceName = "phone" + String.valueOf(i);
            }

            // 尝试获取服务看是否能获取到
            IBinder iBinder = ServiceManager.getService(phoneServiceName);
            if(iBinder == null) continue;

            ITelephony iTelephony = ITelephony.Stub.asInterface(iBinder);
            if(iTelephony == null) continue;

            phoneServerList.add(phoneServiceName);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }
    // 这个是默认的
    phoneServerList.add(Context.TELEPHONY_SERVICE);
    return phoneServerList;
}
复制代码

2、挂断电话

  挂断电话使用系统服务提供的接口去挂断,但是挂断电话是个并不能保证成功的方法,所以会有多种方式挂断同时使用,下面提供

复制代码
public boolean endCall() {
    boolean callSuccess = false;
    ITelephony telephonyService = getTelephonyService();
    try {
        if (telephonyService != null) {
            callSuccess = telephonyService.endCall();
        }
    } catch (RemoteException e) {
        e.printStackTrace();
    } catch (Exception e){
        e.printStackTrace();
    }
    if (callSuccess == false) {
        Executor eS = Executors.newSingleThreadExecutor();
        eS.execute(new Runnable() {
            @Override
            public void run() {
                disconnectCall();
            }
        });
        callSuccess = true;
    }
    return callSuccess;
}

private boolean disconnectCall() {
    Runtime runtime = Runtime.getRuntime();
    try {
        runtime.exec("service call phone 5 \n");
    } catch (Exception exc) {
        exc.printStackTrace();
        return false;
    }
    return true;
}

// 使用endCall挂断不了,再使用killCall反射调用再挂一次
public static boolean killCall(Context context) {
    try {
        // Get the boring old TelephonyManager
          TelephonyManager telephonyManager = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

        // Get the getITelephony() method
          Class classTelephony = Class.forName(telephonyManager.getClass().getName());
        Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

        // Ignore that the method is supposed to be private
          methodGetITelephony.setAccessible(true);

        // Invoke getITelephony() to get the ITelephony interface
          Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

        // Get the endCall method from ITelephony
          Class telephonyInterfaceClass = Class.forName(telephonyInterface.getClass().getName());
        Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");

        // Invoke endCall()
        methodEndCall.invoke(telephonyInterface);
    } catch (Exception ex) { // Many things can go wrong with reflection calls
        return false;
    }
    return true;
}
复制代码

 

3、挂断电话需要权限

<uses-permission android:name="android.permission.CALL_PHONE" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值