Android-9種通話狀態(精確)
阿新 • • 發佈:2019-01-25
一.介紹
在應用層監聽通話狀態只有三種,從TelephonyManager.java中註釋可知這三種狀態含義如下:
CALL_STATE_IDLE 空閒態(沒有通話活動)
CALL_STATE_RINGING 包括響鈴、第三方來電等待
CALL_STATE_OFFHOOK 包括dialing撥號中、active接通、hold掛起等
由上可知,active接通狀態沒有單獨給出,所以我們無法得知電話是否接通了,
因此需要其它手段來獲取更多的精確通話狀態,遍查網路資料,一般有兩種方法!
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;
}
二.監聽9種通話狀態
法一.使用系統api監聽
條件:
1.需要許可權android.permission.READ_PRECISE_PHONE_STATE、app打包時需要系統簽名、安裝在系統目錄等
2.onPreciseCallStateChanged 精確通話回撥api在android.jar中被hide了, 可以使用反射或沒有被hide的android.jar解決
TelephonyManager telM = (TelephonyManager) getSystemService(TELEPHONY_SERVICE);
telM.listen(new PhoneStateListener(){
/**
* 當有精確通話狀態時回撥
* Callback invoked when precise device call state changes
* @hide 隱藏api,給系統app使用的
*/
@Override
public void onPreciseCallStateChanged(PreciseCallState callState) {
//當有精確通話狀態時回撥
}
}, PhoneStateListener.LISTEN_PRECISE_CALL_STATE); //需要許可權android.permission.READ_PRECISE_PHONE_STATE
// 精確的九大通話狀態
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; //通話掛起(例如我和多個人通話,其中一個通話在活動,而其它通話就會進入掛起狀態)
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; //第三方來電等待(例如我正在和某人通話,而其他人打入時就會就進入等待狀態)
public static final int PRECISE_CALL_STATE_DISCONNECTED = 7; //結束通話完成
public static final int PRECISE_CALL_STATE_DISCONNECTING = 8; //正在結束通話
}
法二.讀取Logcat通訊日誌
條件:
1.android 4.1以上需要root許可權,android 4.1以下版本只需新增日誌許可權android.permission.READ_LOGS
2.讀取通訊狀態:在root狀態下執行命令 logcat -v time -b radio
logcat日誌被劃分為以下幾個緩衝區
-b <system, radio, events, main>
main — 主日誌緩衝區(預設,普通app應用)
radio — 無線/電話相關日誌緩衝區
events — 事件相關日誌緩衝區
system — 系統相關日誌緩衝區
//正則表示式,匹配通話狀態
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許可權,執行logcat命令
Process process = Runtime.getRuntime().exec("su");
PrintWriter pw = new PrintWriter(process.getOutputStream());
pw.println("logcat -v time -b radio"); //logcat命令, -v 詳細時間; -b radio 通訊相關日誌緩衝區
pw.flush();
//迴圈讀取通話日誌
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String strLine;
while ((strLine = br.readLine()) != null) {
Matcher matcher = ptn.matcher(strLine);
if (matcher.find()) {// 匹配結果
String time = matcher.group(1); //提取通話時間
String state = matcher.group(2); //提取通話狀態
}
}
pw.close();
br.close();
process.destroy();