android 来电状态

引用:http://blog.csdn.net/shaojie519/article/details/6764737

无论是打电话还是来电,系统广播是会发送相应的消息的,我们可以监听广播获取手机电话状态。

首先在manifest文件中配置自己的BroadcastReceiver,和声明权限。

 

  1. <receiver android:name=".filter.PhoneStatReceiver">    
  2.         <intent-filter>  
  3.              <action android:name="android.intent.action.PHONE_STATE"/>             
  4.              <action android:name="android.intent.action.NEW_OUTGOING_CALL" />  
  5.         </intent-filter>  
  6. </receiver>  
  7.   
  8.  <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>  
  9.  <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS"></uses-permission>  


在自己的BroadcastReceiver中做相应的处理

 

 

    1. import android.app.Service;  
    2. import android.content.BroadcastReceiver;  
    3. import android.content.Context;  
    4. import android.content.Intent;  
    5. import android.telephony.TelephonyManager;  
    6. import android.util.Log;  
    7.   
    8.   
    9. public class PhoneStatReceiver extends BroadcastReceiver{  
    10.           
    11.         private static final String TAG = "PhoneStatReceiver";  
    12.           
    13.         private static boolean incomingFlag = false;  
    14.           
    15.         private static String incoming_number = null;  
    16.   
    17.         @Override  
    18.         public void onReceive(Context context, Intent intent) {  
    19.                 //如果是拨打电话  
    20.                 if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                          
    21.                         incomingFlag = false;  
    22.                         String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);          
    23.                         Log.i(TAG, "call OUT:"+phoneNumber);                          
    24.                 }else{                          
    25.                         //如果是来电  
    26.                         TelephonyManager tm =   
    27.                             (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                          
    28.                           
    29.                         switch (tm.getCallState()) {  
    30.                         case TelephonyManager.CALL_STATE_RINGING:  
    31.                                 incomingFlag = true;//标识当前是来电  
    32.                                 incoming_number = intent.getStringExtra("incoming_number");  
    33.                                 Log.i(TAG, "RINGING :"+ incoming_number);  
    34.                                 break;  
    35.                         case TelephonyManager.CALL_STATE_OFFHOOK:  //存在至少有一个呼叫拨号,活跃,或                                                                    //保留,并没有来电振铃或等待                                
    36.                                 if(incomingFlag){  
    37.                                         Log.i(TAG, "incoming ACCEPT :"+ incoming_number);  
    38.                                 }  
    39.                                 break;  
    40.                           
    41.                         case TelephonyManager.CALL_STATE_IDLE:   //闲置状态                       
    42.                                 if(incomingFlag){  
    43.                                         Log.i(TAG, "incoming IDLE");                                  
    44.                                 }  
    45.                                 break;  
    46.                         }   
    47.                 }  
    48.         }  
    49. }  
posted @ 2013-03-09 01:52  镇水古月  阅读(286)  评论(0编辑  收藏  举报