Android 电话状态的监听(来电和去电)

jopen 10年前

原文:http://www.cnblogs.com/haowenbiao/archive/2012/08/15/2639579.html

 

实现手机电话状态的监听,主要依靠两个类:TelephoneManger和PhoneStateListener。 
TelephonseManger提供了取得手机基本服务的信息的一种方式。因此应用程序可以使用TelephonyManager来探测手机基本服务的情况。应用程序可以注册listener来监听电话状态的改变。我们不能对TelephonyManager进行实例化,只能通过获取服务的形式:
Context.getSystemService(Context.TELEPHONY_SERVICE);
注意:对手机的某些信息进行读取是需要一定许可(permission)的。
 
主要静态成员常量:(它们对应PhoneStateListener.LISTEN_CALL_STATE所监听到的内容)
int CALL_STATE_IDLE   空闲状态,没有任何活动。
int CALL_STATE_OFFHOOK  摘机状态,至少有个电话活动。该活动或是拨打(dialing)或是通话,或是 on hold。并且没有电话是ringing or waiting
int CALL_STATE_RINGING  来电状态,电话铃声响起的那段时间或正在通话又来新电,新来电话不得不等待的那段时间。
 
手机通话状态在广播中的对应值
 
EXTRA_STATE_IDLE 它在手机通话状态改变的广播中,用于表示CALL_STATE_IDLE状态
EXTRA_STATE_OFFHOOK 它在手机通话状态改变的广播中,用于表示CALL_STATE_OFFHOOK状态
EXTRA_STATE_RINGING 它在手机通话状态改变的广播中,用于表示CALL_STATE_RINGING状态
ACTION_PHONE_STATE_CHANGED 在广播中用ACTION_PHONE_STATE_CHANGED这个Action来标示通话状态改变的广播(intent)。
注:需要许可READ_PHONE_STATE。
String EXTRA_INCOMING_NUMBER  
在手机通话状态改变的广播,用于从extra取来电号码。
String EXTRA_STATE  在通话状态改变的广播,用于从extra取来通话状态。
 
主要成员函数
public int getCallState() 取得手机的通话状态。
public CellLocation getCellLocation () 返回手机当前所处的位置。如果当前定位服务不可用,则返回null
注:需要许可(Permission)ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION.
public int getDataActivity () 返回当前数据连接活动状态的情况。
public int getDataState () 返回当前数据连接状态的情况。
public String getDeviceId ()
返回手机的设备ID。比如对于GSM的手机来说是IMEI码,对于CDMA的手机来说MEID码或ESN码。如果读取失败,则返回null。
 
如何实现电话状态的监听呢?
 
Android在电话状态改变是会发送action为android.intent.action.PHONE_STATE的广播,而拨打电话时会发送 action为android.intent.action.NEW_OUTGOING_CALL的广播,但是我看了下开发文档,暂时没发现有来电时的广播。通过自定义广播接收器,接受上述两个广播便可。
 
Java代码:
package com.pocketdigi.phonelistener;
 
import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
 
public class PhoneReceiver extends BroadcastReceiver {
 
 @Override
 public void onReceive(Context context, Intent intent) {
  System.out.println("action"+intent.getAction());
  //如果是去电
  if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){
     String phoneNumber = intent
     .getStringExtra(Intent.EXTRA_PHONE_NUMBER);
     Log.d(TAG, "call OUT:" + phoneNumber);  
   }else{
   //查了下android文档,貌似没有专门用于接收来电的action,所以,非去电即来电.
   //如果我们想要监听电话的拨打状况,需要这么几步 :
    第一:获取电话服务管理器TelephonyManager manager = this.getSystemService(TELEPHONY_SERVICE);
   
第二:通过TelephonyManager注册我们要监听的电话状态改变事件。manager.listen(new MyPhoneStateListener(),
    PhoneStateListener.LISTEN_CALL_STATE);这里的PhoneStateListener.LISTEN_CALL_STATE就是我们想要 
   
监听的状态改变事件,初次之外,还有很多其他事件哦。
    第三步:通过extends PhoneStateListener来定制自己的规则。将其对象传递给第二步作为参数。
   
第四步:这一步很重要,那就是给应用添加权限。android.permission.READ_PHONE_STATE




   TelephonyManager tm = (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);  
   tm.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
   //设置一个监听器
  }
 }
 PhoneStateListener listener=new PhoneStateListener(){
 
  @Override
  public void onCallStateChanged(int state, String incomingNumber) {
   //注意,方法必须写在super方法后面,否则incomingNumber无法获取到值。
   super.onCallStateChanged(state, incomingNumber);
   switch(state){
   case TelephonyManager.CALL_STATE_IDLE:
    System.out.println("挂断");
    break;
   case TelephonyManager.CALL_STATE_OFFHOOK:
    System.out.println("接听");
    break;
   case TelephonyManager.CALL_STATE_RINGING:
    System.out.println("响铃:来电号码"+incomingNumber);
    //输出来电号码
    break;
   }
  }
 };
}
要在AndroidManifest.xml注册广播接收器: 
<receiver android:name=".PhoneReceiver">   
        <intent-filter>   
            <action android:name="android.intent.action.PHONE_STATE"/>   
    <action android:name="android.intent.action.NEW_OUTGOING_CALL" />   
        </intent-filter>   
       </receiver>   
 <receiver android:name=".PhoneReceiver"> <intent-filter> <action android:name="android.intent.action.PHONE_STATE"/> <action android:name="android.intent.action.NEW_OUTGOING_CALL" /> </intent-filter> </receiver>


还要添加权限: 
<uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>   
 <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>