[AndroidTips]Android监听来电和去电

参考: 
android 呼入电话的监听(来电监听)
http://stephen830.iteye.com/blog/1181010 
android 呼出电话的监听(去电监听)
http://stephen830.iteye.com/blog/1181452

android-轻松监听来电和去电
http://www.eoeandroid.com/thread-8994-1-1.html

 

android 呼入电话的监听(来电监听)

 

需要权限:

Xml代码   收藏代码
  1. <uses-permission android:name="android.permission.READ_PHONE_STATE" />  

 

方式一:通过广播接收来电

 

定义来电广播接收类

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.telephony.TelephonyManager;  
  7. import android.util.Log;  
  8.   
  9. public class BroadcastReceiverMgr extends BroadcastReceiver {  
  10.       
  11.     private final String TAG = MyBroadcastReceiver.TAG;  
  12.   
  13.     @Override  
  14.     public void onReceive(Context context, Intent intent) {  
  15.         String action = intent.getAction();  
  16.         Log.i(TAG, "[Broadcast]"+action);  
  17.           
  18.         //呼入电话  
  19.         if(action.equals(MyBroadcastReceiver.B_PHONE_STATE)){  
  20.             Log.i(TAG, "[Broadcast]PHONE_STATE");  
  21.             doReceivePhone(context,intent);  
  22.         }  
  23.     }  
  24.       
  25.     /** 
  26.      * 处理电话广播. 
  27.      * @param context 
  28.      * @param intent 
  29.      */  
  30.     public void doReceivePhone(Context context, Intent intent) {  
  31.         String phoneNumber = intent.getStringExtra(  
  32. TelephonyManager.EXTRA_INCOMING_NUMBER);  
  33.         TelephonyManager telephony =   
  34. (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);  
  35.         int state = telephony.getCallState();  
  36.         switch(state){  
  37.         case TelephonyManager.CALL_STATE_RINGING:  
  38.             Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);  
  39.             break;  
  40.         case TelephonyManager.CALL_STATE_IDLE:  
  41.             Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);  
  42.             break;  
  43.         case TelephonyManager.CALL_STATE_OFFHOOK:  
  44.             Log.i(TAG, "[Broadcast]通话中="+phoneNumber);  
  45.             break;  
  46.         }  
  47.     }  
  48. }  

 

定义Actitvity类

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7. import android.os.Bundle;  
  8. import android.telephony.PhoneStateListener;  
  9. import android.telephony.TelephonyManager;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12.   
  13. public class MyBroadcastReceiver extends Activity {  
  14.     public final static String TAG = "MyBroadcastReceiver";  
  15.       
  16.     public final static String B_PHONE_STATE =   
  17. TelephonyManager.ACTION_PHONE_STATE_CHANGED;  
  18.       
  19.     private BroadcastReceiverMgr mBroadcastReceiver;  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.my_broadcast_receiver);  
  25.     }     
  26.   
  27.     //按钮1-注册广播  
  28.     public void registerIt(View v) {  
  29.         Log.i(TAG, "registerIt");  
  30.         mBroadcastReceiver = new BroadcastReceiverMgr();  
  31.         IntentFilter intentFilter = new IntentFilter();  
  32.         intentFilter.addAction(B_PHONE_STATE);  
  33.         intentFilter.setPriority(Integer.MAX_VALUE);  
  34.         registerReceiver(mBroadcastReceiver, intentFilter);  
  35.     }  
  36.       
  37.     //按钮2-撤销广播  
  38.     public void unregisterIt(View v) {  
  39.         Log.i(TAG, "unregisterIt");  
  40.         unregisterReceiver(mBroadcastReceiver);  
  41.     }  
  42.       
  43. }  

 

方式二:通过监听器来实现

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7. import android.os.Bundle;  
  8. import android.telephony.PhoneStateListener;  
  9. import android.telephony.TelephonyManager;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12.   
  13. public class MyBroadcastReceiver extends Activity {  
  14.     public final static String TAG = "MyBroadcastReceiver";  
  15.       
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.my_broadcast_receiver);  
  20.     }  
  21.       
  22.     /** 
  23.      * 按钮-监听电话 
  24.      * @param v 
  25.      */  
  26.     public void createPhoneListener(View v) {  
  27.         TelephonyManager telephony = (TelephonyManager)getSystemService(  
  28. Context.TELEPHONY_SERVICE);  
  29.         telephony.listen(new OnePhoneStateListener(),  
  30. PhoneStateListener.LISTEN_CALL_STATE);  
  31.     }  
  32.       
  33.     /** 
  34.      * 电话状态监听. 
  35.      * @author stephen 
  36.      * 
  37.      */  
  38.     class OnePhoneStateListener extends PhoneStateListener{  
  39.         @Override  
  40.         public void onCallStateChanged(int state, String incomingNumber) {  
  41.             Log.i(TAG, "[Listener]电话号码:"+incomingNumber);  
  42.             switch(state){  
  43.             case TelephonyManager.CALL_STATE_RINGING:  
  44.                 Log.i(TAG, "[Listener]等待接电话:"+incomingNumber);  
  45.                 break;  
  46.             case TelephonyManager.CALL_STATE_IDLE:  
  47.                 Log.i(TAG, "[Listener]电话挂断:"+incomingNumber);  
  48.                 break;  
  49.             case TelephonyManager.CALL_STATE_OFFHOOK:  
  50.                 Log.i(TAG, "[Listener]通话中:"+incomingNumber);  
  51.                 break;  
  52.             }  
  53.             super.onCallStateChanged(state, incomingNumber);  
  54.         }  
  55.     }  
  56.       
  57.       
  58.   
  59.       
  60. }  

 

 

android 呼出电话的监听(去电监听)

 

权限:

Xml代码   收藏代码
  1. <uses-permission android:name="android.permission.PROCESS_OUTGOING_CALLS" />  

 

通过接收呼出电话的广播来实现

定义广播类

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.content.BroadcastReceiver;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.telephony.TelephonyManager;  
  7. import android.util.Log;  
  8.   
  9. public class BroadcastReceiverMgr extends BroadcastReceiver {  
  10.       
  11.     private final String TAG = MyBroadcastReceiver.TAG;  
  12.   
  13.     @Override  
  14.     public void onReceive(Context context, Intent intent) {  
  15.         String action = intent.getAction();  
  16.         Log.i(TAG, "[Broadcast]"+action);  
  17.           
  18.   
  19.           
  20.         //呼出电话  
  21.         if(action.equals(MyBroadcastReceiver.B_ACTION_NEW_OUTGOING_CALL)){  
  22.             String outPhoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);  
  23.             Log.i(TAG, "[Broadcast]ACTION_NEW_OUTGOING_CALL:"+outPhoneNumber);  
  24.             //this.setResultData(null);  
  25. //这里可以更改呼出电话号码。如果设置为null,电话就永远不会播出了.  
  26.         }  
  27.           
  28.           
  29.     }  
  30.       
  31.       
  32. }  

 

定义activity类

Java代码   收藏代码
  1. package com.zhouzijing.android.demo;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Context;  
  5. import android.content.Intent;  
  6. import android.content.IntentFilter;  
  7. import android.os.Bundle;  
  8. import android.telephony.PhoneStateListener;  
  9. import android.telephony.TelephonyManager;  
  10. import android.util.Log;  
  11. import android.view.View;  
  12.   
  13. public class MyBroadcastReceiver extends Activity {  
  14.     public final static String TAG = "MyBroadcastReceiver";  
  15.       
  16.       
  17.     public final static String B_ACTION_NEW_OUTGOING_CALL = Intent.ACTION_NEW_OUTGOING_CALL;  
  18.       
  19.     private BroadcastReceiverMgr mBroadcastReceiver;  
  20.       
  21.     @Override  
  22.     public void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         setContentView(R.layout.my_broadcast_receiver);  
  25.     }  
  26.       
  27.       
  28.       
  29.       
  30.   
  31.     //按钮1-注册广播  
  32.     public void registerIt(View v) {  
  33.         Log.i(TAG, "registerIt");  
  34.         mBroadcastReceiver = new BroadcastReceiverMgr();  
  35.         IntentFilter intentFilter = new IntentFilter();  
  36.         intentFilter.addAction(Intent.ACTION_NEW_OUTGOING_CALL);  
  37.         intentFilter.setPriority(Integer.MAX_VALUE);  
  38.         registerReceiver(mBroadcastReceiver, intentFilter);  
  39.     }  
  40.       
  41.     //按钮2-撤销广播  
  42.     public void unregisterIt(View v) {  
  43.         Log.i(TAG, "unregisterIt");  
  44.         unregisterReceiver(mBroadcastReceiver);  
  45.     }  
  46.       
  47. }  

 

 

 

---------------------------------------------------------------------------------------------------------

要监听android打电话和接电话,只需下面2步骤

1.第一步,写一个Receiver继承自BroadcastReceiver

Java代码   收藏代码
  1. public class PhoneStatReceiver extends BroadcastReceiver{  
  2.           
  3.         private static final String TAG = "PhoneStatReceiver";  
  4.           
  5. //        private static MyPhoneStateListener phoneListener = new MyPhoneStateListener();  
  6.           
  7.         private static boolean incomingFlag = false;  
  8.           
  9.         private static String incoming_number = null;  
  10.   
  11.         @Override  
  12.         public void onReceive(Context context, Intent intent) {  
  13.                 //如果是拨打电话  
  14.                 if(intent.getAction().equals(Intent.ACTION_NEW_OUTGOING_CALL)){                          
  15.                         incomingFlag = false;  
  16.                         String phoneNumber = intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER);          
  17.                         Log.i(TAG, "call OUT:"+phoneNumber);                          
  18.                 }else{                          
  19.                         //如果是来电  
  20.                         TelephonyManager tm =   
  21.                             (TelephonyManager)context.getSystemService(Service.TELEPHONY_SERVICE);                          
  22.                           
  23.                         switch (tm.getCallState()) {  
  24.                         case TelephonyManager.CALL_STATE_RINGING:  
  25.                                 incomingFlag = true;//标识当前是来电  
  26.                                 incoming_number = intent.getStringExtra("incoming_number");  
  27.                                 Log.i(TAG, "RINGING :"+ incoming_number);  
  28.                                 break;  
  29.                         case TelephonyManager.CALL_STATE_OFFHOOK:                                  
  30.                                 if(incomingFlag){  
  31.                                         Log.i(TAG, "incoming ACCEPT :"+ incoming_number);  
  32.                                 }  
  33.                                 break;  
  34.                           
  35.                         case TelephonyManager.CALL_STATE_IDLE:                                  
  36.                                 if(incomingFlag){  
  37.                                         Log.i(TAG, "incoming IDLE");                                  
  38.                                 }  
  39.                                 break;  
  40.                         }   
  41.                 }  
  42.         }  
  43. }  

 

第二步:在AndroidManifest.xml,配置写好的Receiver,并拦截相应的BroadCastAction,
另外注意加上相应的权限。

Xml代码   收藏代码
  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>   
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值