參考文章:
http://blog.csdn.net/yaoming168/article/details/51986751
http://blog.csdn.net/a34140974/article/details/50156193
一.介紹
在應用層監(jiān)聽通話狀態(tài)只有三種,從TelephonyManager.java中注釋可知這三種狀態(tài)含義如下:
CALL_STATE_IDLE 空閑態(tài)(沒有通話活動)
CALL_STATE_RINGING 包括響鈴恭朗、第三方來電等待
CALL_STATE_OFFHOOK 包括dialing撥號中赐稽、active接通、hold掛起等
由上可知凡纳,active接通狀態(tài)沒有單獨給出,所以我們無法得知電話是否接通了,
因此需要其它手段來獲取更多的精確通話狀態(tài)两芳,遍查網(wǎng)絡資料撵孤,一般有兩種方法迈着!
public class TelephonyManager {
/** 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;
}
二.監(jiān)聽9種通話狀態(tài)
法一.使用系統(tǒng)api監(jiān)聽
條件:
1.需要權(quán)限android.permission.READ_PRECISE_PHONE_STATE、app打包時需要系統(tǒng)簽名邪码、安裝在系統(tǒng)目錄等
2.onPreciseCallStateChanged 精確通話回調(diào)api在android.jar中被hide了, 可以使用反射或沒有被hide的android.jar解決
TelephonyManager telM = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telM.listen(new PhoneStateListener(){
/**
* 當有精確通話狀態(tài)時回調(diào)
* Callback invoked when precise device call state changes
* @hide 隱藏api,給系統(tǒng)app使用的
*/
@Override
public void onPreciseCallStateChanged(PreciseCallState callState) {
//當有精確通話狀態(tài)時回調(diào)
}
}, PhoneStateListener.LISTEN_PRECISE_CALL_STATE); //需要權(quán)限android.permission.READ_PRECISE_PHONE_STATE
// 精確的九大通話狀態(tài)
public class PreciseCallState implements Parcelable {
public static final int PRECISE_CALL_STATE_IDLE = 0; //通話空閑
public static final int PRECISE_CALL_STATE_ACTIVE = 1; //正在通話(活動中)
public static final int PRECISE_CALL_STATE_HOLDING = 2; //通話掛起(例如我和多個人通話,其中一個通話在活動,而其它通話就會進入掛起狀態(tài))
public static final int PRECISE_CALL_STATE_DIALING = 3; //撥號開始
public static final int PRECISE_CALL_STATE_ALERTING = 4; //正在呼出(提醒對方接電話)
public static final int PRECISE_CALL_STATE_INCOMING = 5; //對方來電
public static final int PRECISE_CALL_STATE_WAITING = 6; //第三方來電等待(例如我正在和某人通話,而其他人打入時就會就進入等待狀態(tài))
public static final int PRECISE_CALL_STATE_DISCONNECTED = 7; //掛斷完成
public static final int PRECISE_CALL_STATE_DISCONNECTING = 8; //正在掛斷
}
法二.讀取Logcat通信日志
條件:
1.android 4.1以上需要root權(quán)限裕菠,android 4.1以下版本只需添加日志權(quán)限android.permission.READ_LOGS
2.讀取通信狀態(tài):在root狀態(tài)下執(zhí)行命令 logcat -v time -b radio
logcat日志被劃分為以下幾個緩沖區(qū)
-b <system, radio, events, main>
main — 主日志緩沖區(qū)(默認,普通app應用)
radio — 無線/電話相關(guān)日志緩沖區(qū)
events — 事件相關(guān)日志緩沖區(qū)
system — 系統(tǒng)相關(guān)日志緩沖區(qū)
//正則表達式,匹配通話狀態(tài)
Pattern ptn = Pattern.compile("(\\d{2}\\-\\d{2}\\s\\d{2}\\:\\d{2}\\:\\d{2}\\.\\d{3}).*?GET_CURRENT_CALLS.*?,(\\w+),");
//Pattern ptn = Pattern.compile("(\\d{2}-\\d{2}\\s\\d{2}:\\d{2}:\\d{2}\\.\\d{3}).*?qcril_qmi_voice_all_call_status_ind_hdlr:.call.state.(\\d),");
//使用Root權(quán)限闭专,執(zhí)行l(wèi)ogcat命令
Process process = Runtime.getRuntime().exec("su");
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.println("logcat -v time -b radio"); //logcat命令, -v 詳細時間; -b radio 通信相關(guān)日志緩沖區(qū)
pw.flush();
//循環(huán)讀取通話日志
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String strLine;
while ((strLine = br.readLine()) != null) {
Matcher matcher = ptn.matcher(strLine);
if (matcher.find()) {// 匹配結(jié)果
String time = matcher.group(1); //提取通話時間
String state = matcher.group(2); //提取通話狀態(tài)
}
}
pw.close();
br.close();
process.destroy();
三.圖解9種通話狀態(tài)
簡書: http://www.reibang.com/p/a362404f850f
CSDN博客: http://blog.csdn.net/qq_32115439/article/details/78395537
GitHub博客: http://lioil.win/2017/10/30/Android-PhoneState.html
Coding博客: http://c.lioil.win/2017/10/30/Android-PhoneState.html