[AndroidTips]Android监听来电和去电

2019-04-13 21:13发布

参考: 
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