为了账号安全,请及时绑定邮箱和手机立即绑定

安卓接听电话类型(蓝牙,耳机,听筒,免提)的判断

标签:
Android

前段时间项目中有一个需求,获取用户接听电话时的接听类型,(蓝牙,耳机,听筒,外放),听到这个需求时,第一个想到的就是,先用广播接受者,判断是否正在接听电话。这里我用到的是PhoneStateListener,具体用法如下:
1 获取TelephonyManaget

        tm = (TelephonyManager) getSystemService(Service.TELEPHONY_SERVICE);

2 注册监听

      myPhoneStateListener = new MyPhoneStateListener();
        tm.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);

3 编写监听程序(判断来电或者去电)

 public class MyPhoneStateListener extends PhoneStateListener {        @Override
        public void onCallStateChanged(int state, String incomingNumber) {            super.onCallStateChanged(state, incomingNumber);
            isRecord = myApplication.isRecord();            switch (state) {                case TelephonyManager.CALL_STATE_IDLE://挂断
                    break;                case TelephonyManager.CALL_STATE_OFFHOOK://接听
                    break;                case TelephonyManager.CALL_STATE_RINGING://响铃
                    break;
            }
        }
    }

但是现在问题来了,怎么判断用户接听时的类型,android 中好像没有这样判断接听类型的广播,后来在查找资料的过程中发现,接听电话时声音的播放是通过AudioManager这个类来管理的,于是问题就得到解决了。
先来看看代码中要调用的函数
1 audioManager.isWiredHeadsetOn() 这个方法返回值为boolean,用来判断是否插入耳机
2 audioManager.isBluetoothScoOn() 这个方法返回值为boolean,用来判断输出声音是否通过蓝牙
3 audioManager.isSpeakerphoneOn() 这个方法返回值为boolean,用来判断输出声音是否通过扬声器

最后的代码

  public class MyPhoneStateListener extends PhoneStateListener {        @Override
        public void onCallStateChanged(int state, String incomingNumber) {            super.onCallStateChanged(state, incomingNumber);
            isRecord = myApplication.isRecord();            if (isRecord) {
                initFile();
                initWriter();                switch (state) {                    case TelephonyManager.CALL_STATE_IDLE://挂断
                        sw = false;
                        saveEventModuleToFile(getCurrentTime(), "CALL_STATE_IDLE");                        break;                    case TelephonyManager.CALL_STATE_OFFHOOK://接听
                        Log.d("zw", "接听");                        new Thread() {                            public void run() {                                super.run();
                                sw = true;
                                String res = "";
                                String te = "";
                                isheadin = audioManager.isWiredHeadsetOn();//可以注册HeadsetPlugReceiver来判断耳机的插拔,来改变isHeadin的值
                                while (sw) {//一个while循环 在挂断时改变sw的值,跳出循环
                                    if (audioManager.isBluetoothScoOn()) {//判断是否经过蓝牙
                                        res = "蓝牙";
                                    }                                    if (audioManager.isSpeakerphoneOn()) {
                                        res = "扬声器";//判断是否经过扬声器
                                    }                                    if (!audioManager.isBluetoothScoOn() && !audioManager.isSpeakerphoneOn()) {//如果既没有经过蓝牙 也没有经过扬声器 则判断耳机还是听筒
                                        if (isheadin) {// isheadin的值通过HeadsetPlugReceiver(注册广播,监听耳机插拔事件)来判断,
                                            res = "耳机";
                                        } else {
                                            res = "听筒";
                                        }
                                    }                                    if (!res.trim().equals(te.trim())) {                                        if (res.equals("耳机")) {
                                            Log.d("zw", "耳机");
                                            saveEventModuleToFile(getCurrentTime(), "TYPE_CALL_HEAD");
                                        }                                        if (res.equals("听筒")) {
                                            Log.d("zw", "听筒");

                                            saveEventModuleToFile(getCurrentTime(), "TYPE_CALL_EAR");
                                        }                                        if (res.equals("蓝牙")) {
                                            Log.d("zw", "蓝牙");

                                            saveEventModuleToFile(getCurrentTime(), "TYPE_CALL_BlUETOOCH");
                                        }                                        if (res.equals("扬声器")) {
                                            Log.d("zw", "扬声器");

                                            saveEventModuleToFile(getCurrentTime(), "TYPE_CALL_SPEAKER");
                                        }
                                    }

                                    te = res;
                                }
                            }
                        }.start();
                        saveEventModuleToFile(getCurrentTime(), "CALL_STATE_OFFHOOK");                        break;                    case TelephonyManager.CALL_STATE_RINGING://响铃
                        Log.d("zw", "响铃:来电号码 " + incomingNumber);
                        saveEventModuleToFile(getCurrentTime(), "CALL_STATE_RINGING");                        break;
                }
            }
        }
    }

步骤有点繁琐,有更好方法的的小伙伴们,欢迎留言交流

原文链接:http://www.apkbus.com/blog-940038-76557.html

点击查看更多内容
TA 点赞

若觉得本文不错,就分享一下吧!

评论

作者其他优质文章

正在加载中
  • 推荐
  • 评论
  • 收藏
  • 共同学习,写下你的评论
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦
今天注册有机会得

100积分直接送

付费专栏免费学

大额优惠券免费领

立即参与 放弃机会
意见反馈 帮助中心 APP下载
官方微信

举报

0/150
提交
取消