Android监听通话正确操作方法介绍

移动开发 Android
Android监听通话的实现,将会在这篇文章中通过一段代码为大家详细介绍。希望这一应用技巧可以使大家在实际应用中起到一些作用。

对智能手机有所了解的朋友都知道其中一个应用广泛的手机操作系统Android 开源手机操作系统。那么在这一系统中想要实现通话的监听功能的话,我们应当如何操作呢?在这里就为大家详细介绍了Android监听通话的相关实现方法。#t#

开发应用程序的时候,我们希望能够监听电话的呼入,以便执行暂停音乐播放器等操作,当电话结束之后,再次恢复播放。在Android平台可以通过TelephonyManager和PhoneStateListener来完成此任务。

 

TelephonyManager作为一个Service接口提供给用户查询电话相关的内容,比如IMEI,LineNumber1等。通过下面的代码即可获得TelephonyManager的实例。

 

  1. TelephonyManager mTelephonyMgr = (TelephonyManager) this  
  2. .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监听通话。

 

 

  1. mTelephonyMgr.listen(new TeleListener(),  
  2. 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监听通话的具体操作方法就为大家介绍到这里。

责任编辑:曹凯 来源: javaeye.com
相关推荐

2010-02-23 17:59:52

WSIT连接WCF

2010-01-27 14:08:56

Android查询联系

2009-12-30 14:28:06

Silverlight

2010-03-04 14:32:24

Python自动下载文

2009-12-30 15:53:28

Silverlight

2009-12-28 17:48:01

WPF界面布局

2010-03-05 10:36:52

Python调用zip

2010-01-26 17:36:17

Android实现全屏

2010-01-04 16:50:04

Silverlight

2010-01-07 10:46:27

VB.NET Sock

2010-01-06 17:12:26

.Net Framew

2010-01-26 17:18:13

Android读写文件

2011-07-20 15:08:22

C++

2009-12-15 13:59:42

Ruby对象操作

2010-02-01 09:40:08

Python操作

2010-01-27 18:00:57

Android开机自启

2010-03-04 09:58:32

安装Python

2010-03-05 13:48:24

Python for

2009-12-31 11:35:20

Silverlight

2009-09-18 10:58:31

C#数组操作
点赞
收藏

51CTO技术栈公众号