Android去电监听(二)

2019-04-14 21:46发布

上面提到在系统应用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 + " "+ 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