溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android中如何監聽通話

發布時間:2022-04-16 16:09:59 來源:億速云 閱讀:816 作者:iii 欄目:開發技術

這篇“Android中如何監聽通話”文章的知識點大部分人都不太理解,所以小編給大家總結了以下內容,內容詳細,步驟清晰,具有一定的借鑒價值,希望大家閱讀完這篇文章能有所收獲,下面我們一起來看看這篇“Android中如何監聽通話”文章吧。

TelephonyManager作為一個Service接口提供給用戶查詢電話相關的內容,比如IMEI,LineNumber1等。通過下面的代碼即可獲得TelephonyManager的實例。

TelephonyManager mTelephonyMgr = (TelephonyManager) this  .getSystemService(Context.TELEPHONY_SERVICE);

在Android平臺中,PhoneStateListener是個很有用的監聽器,用來監聽電話的狀態,比如呼叫狀態和連接服務等。Android監聽通話方法如下所示:

  1. public void onCallForwardingIndicatorChanged(boolean cfi)  

  2. public void onCallStateChanged(int state, 
    String incomingNumber)  

  3. public void onCellLocationChanged(CellLocation location)  

  4. public void onDataActivity(int direction)  

  5. public void onDataConnectionStateChanged(int state)  

  6. public void onMessageWaitingIndicatorChanged(boolean mwi)  

  7. public void onServiceStateChanged
    (ServiceState serviceState)  

  8. public void onSignalStrengthChanged(int asu) 

這里我們只需要覆蓋onCallStateChanged()方法即可監聽呼叫狀態。在TelephonyManager中定義了三種狀態,分別是振鈴(RINGING),摘機(OFFHOOK)和空閑(IDLE),我們通過state的值就知道現在的電話狀態了。

獲得了TelephonyManager接口之后,調用listen()方法即可實現Android監聽通話。

mTelephonyMgr.listen(new TeleListener(),  PhoneStateListener.LISTEN_CALL_STATE);

下面是個簡單的測試例子,只是把呼叫狀態追加到TextView之上。

  1. package com.j2medev;  

  2. import android.app.Activity;  

  3. import android.content.Context;  

  4. import android.os.Bundle;  

  5. import android.telephony.PhoneStateListener;  

  6. import android.telephony.TelephonyManager;  

  7. import android.util.Log;  

  8. import android.widget.TextView;  

  9. public class Telephony extends Activity {  

  10. private static final String TAG = "Telephony";  

  11. TextView view = null;  

  12. @Override  

  13. protected void onCreate(Bundle savedInstanceState) {  

  14. super.onCreate(savedInstanceState);  

  15. TelephonyManager mTelephonyMgr = (TelephonyManager) this  

  16. .getSystemService(Context.TELEPHONY_SERVICE);  

  17. mTelephonyMgr.listen(new TeleListener(),  

  18. PhoneStateListener.LISTEN_CALL_STATE);  

  19. view = new TextView(this);  

  20. view.setText("listen the state of phone\n");  

  21. setContentView(view);  

  22. }  

  23. class TeleListener extends PhoneStateListener {  

  24. @Override  

  25. public void onCallStateChanged(int state, 
    String incomingNumber) {  

  26. super.onCallStateChanged(state, incomingNumber);  

  27. switch (state) {  

  28. case TelephonyManager.CALL_STATE_IDLE: {  

  29. Log.e(TAG, "CALL_STATE_IDLE");  

  30. view.append("CALL_STATE_IDLE " + "\n");  

  31. break;  

  32. }  

  33. case TelephonyManager.CALL_STATE_OFFHOOK: {  

  34. Log.e(TAG, "CALL_STATE_OFFHOOK");  

  35. view.append("CALL_STATE_OFFHOOK" + "\n");  

  36. break;  

  37. }  

  38. case TelephonyManager.CALL_STATE_RINGING: {  

  39. Log.e(TAG, "CALL_STATE_RINGING");  

  40. view.append("CALL_STATE_RINGING" + "\n");  

  41. break;  

  42. }  

  43. default:  

  44. break;  

  45. }  

  46. }  

  47. }  

不要忘記在AndroidManifest.xml里面添加個permission.

  1. < uses-permission android:name=
    "android.permission.READ_PHONE_STATE" /> 

以上就是關于“Android中如何監聽通話”這篇文章的內容,相信大家都有了一定的了解,希望小編分享的內容對大家有幫助,若想了解更多相關的知識內容,請關注億速云行業資訊頻道。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女