温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Android中如何监听通话

发布时间:2022-04-16 16:09:59 来源:亿速云 阅读:724 作者: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