继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

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

繁花不似锦
关注TA
已关注
手记 327
粉丝 50
获赞 241

前段时间项目中有一个需求,获取用户接听电话时的接听类型,(蓝牙,耳机,听筒,外放),听到这个需求时,第一个想到的就是,先用广播接受者,判断是否正在接听电话。这里我用到的是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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP