Android去电监听(二)

上面提到在系统应用android:sharedUserId="android.uid.system"下的监听,还是存在各种问题,adb logcat -c不能实时清除日志缓存,后来发现是板子的问题,缓存区要达到一定的大小才可以清除,在各种手机上adb logcat -c都能立即清除缓存,没办法,换种策略读取,网上找了下,借鉴https://www.jianshu.com/p/a362404f850f

public static void readLog() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Pattern ptn = Pattern.compile("(\\d{2}\\-\\d{2}\\s\\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{3}).*?GET_CURRENT_CALLS.*?,(\\w+),");
                 
                    Process process = Runtime.getRuntime().exec("logcat -v time -b radio");
                 //循环读取通话日志
                    BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                    String strLine;
                    while ((strLine = br.readLine()) != null) {
                        Matcher matcher = ptn.matcher(strLine);
                          
                        if (matcher.find()) {// 匹配结果
                            time = matcher.group(1);
                            state = matcher.group(2); //提取通话状态
                            **这里可以输出下state 能够清楚明了通话的各种状态**
                            //开始拨号
                            //由于不能立即清除缓存,将读出来的时间转时间戳跟当前时间比较了,在5秒内说明是当前通话
                            if (TextUtils.equals(state, "ALERTING") && (System.currentTimeMillis() -             date2long(time)) <= 5000) {
                                LogUtil.e(time + "   " + state);
                                User.getInstance().setCalling(true);
                                //通知Activity正在拨号中
                                EventBus.getDefault().post(new Msg(Constant.START_DIALER));

                            }
                            if (TextUtils.equals(state, "ACTIVE") && (System.currentTimeMillis() - date2long(time)) <= 5000) {
                                LogUtil.e(time + "   " + state + "\n"+ strLine  );
                                User.getInstance().setCalling(true);
                                //通知Activity电话接通
                                EventBus.getDefault().post(new Msg(Constant.CALLING));
                                clearLog();
                                break;
                            }
                        }
                    }
                    br.close();
                    process.destroy();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

        }).start();
    }
上面读出来的time 格式03-28 10:08:03.035 
private static long date2long(String date) {
    long ts = 0;
    try {
        date = year + "-" + date.substring(0, date.indexOf("."));
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date dt = simpleDateFormat.parse(date);
        ts = dt.getTime();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ts;
}

static {
    Calendar calendar = Calendar.getInstance();
    year = calendar.get(Calendar.YEAR);
}

至于电话挂断,广播监听...................OK

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值