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

需要权限:

<uses-permission android:name="android.permission.READ_PHONE_STATE" />

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

定义来电广播接收类

package com.zhouzijing.android.demo;import android.content.BroadcastReceiver;import android.content.Context;import android.content.Intent;import android.telephony.TelephonyManager;import android.util.Log;public class BroadcastReceiverMgr extends BroadcastReceiver {private final String TAG = MyBroadcastReceiver.TAG;@Overridepublic void onReceive(Context context, Intent intent) {String action = intent.getAction();Log.i(TAG, "[Broadcast]"+action);//呼入电话if(action.equals(MyBroadcastReceiver.B_PHONE_STATE)){Log.i(TAG, "[Broadcast]PHONE_STATE");doReceivePhone(context,intent);}}/** * 处理电话广播. * @param context * @param intent */public void doReceivePhone(Context context, Intent intent) {String phoneNumber = intent.getStringExtra(TelephonyManager.EXTRA_INCOMING_NUMBER);TelephonyManager telephony = (TelephonyManager)context.getSystemService(Context.TELEPHONY_SERVICE);int state = telephony.getCallState();switch(state){case TelephonyManager.CALL_STATE_RINGING:Log.i(TAG, "[Broadcast]等待接电话="+phoneNumber);break;case TelephonyManager.CALL_STATE_IDLE:Log.i(TAG, "[Broadcast]电话挂断="+phoneNumber);break;case TelephonyManager.CALL_STATE_OFFHOOK:Log.i(TAG, "[Broadcast]通话中="+phoneNumber);break;}}}

定义Actitvity类

package com.zhouzijing.android.demo;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.view.View;public class MyBroadcastReceiver extends Activity {public final static String TAG = "MyBroadcastReceiver";public final static String B_PHONE_STATE = TelephonyManager.ACTION_PHONE_STATE_CHANGED;private BroadcastReceiverMgr mBroadcastReceiver;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_broadcast_receiver);}//按钮1-注册广播public void registerIt(View v) {Log.i(TAG, "registerIt");mBroadcastReceiver = new BroadcastReceiverMgr();IntentFilter intentFilter = new IntentFilter();intentFilter.addAction(B_PHONE_STATE);intentFilter.setPriority(Integer.MAX_VALUE);registerReceiver(mBroadcastReceiver, intentFilter);}//按钮2-撤销广播public void unregisterIt(View v) {Log.i(TAG, "unregisterIt");unregisterReceiver(mBroadcastReceiver);}}

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

package com.zhouzijing.android.demo;import android.app.Activity;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.os.Bundle;import android.telephony.PhoneStateListener;import android.telephony.TelephonyManager;import android.util.Log;import android.view.View;public class MyBroadcastReceiver extends Activity {public final static String TAG = "MyBroadcastReceiver";@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_broadcast_receiver);}/** * 按钮-监听电话 * @param v */public void createPhoneListener(View v) {TelephonyManager telephony = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);telephony.listen(new OnePhoneStateListener(),PhoneStateListener.LISTEN_CALL_STATE);}/** * 电话状态监听. * @author stephen * */class OnePhoneStateListener extends PhoneStateListener{@Overridepublic void onCallStateChanged(int state, String incomingNumber) {Log.i(TAG, "[Listener]电话号码:"+incomingNumber);switch(state){case TelephonyManager.CALL_STATE_RINGING:Log.i(TAG, "[Listener]等待接电话:"+incomingNumber);break;case TelephonyManager.CALL_STATE_IDLE:Log.i(TAG, "[Listener]电话挂断:"+incomingNumber);break;case TelephonyManager.CALL_STATE_OFFHOOK:Log.i(TAG, "[Listener]通话中:"+incomingNumber);break;}super.onCallStateChanged(state, incomingNumber);}}}

更多相关文章

  1. Android 在xml布局配置文件中给Button按钮添加事件
  2. 疯狂android讲义学习总结---各种按钮的制作
  3. android 拖动图片/拖动浮动按钮
  4. android 使用xml定义自己的View
  5. 自定义android RadioButton样式
  6. android拨打电话
  7. php直播源码安卓自定义Dialog设置自动消失
  8. 自定义RatingBar
  9. android中的按钮以图片的方式显示_基础篇

随机推荐

  1. Umeng友盟的小问题 Unable to get provid
  2. VelocityTracker简介
  3. android使用百度地图获取自己所在的城市
  4. Android使用过程中的一些小知识
  5. android java 拷贝数据库文件到U盘,从U盘
  6. 编译android遇到java虚拟机堆内存不够的
  7. Android-将RGB彩×××转换为灰度图
  8. 一起学android之设置资源图片为圆角图片
  9. 新版3.6.1 Android Studio 解决AndroidSt
  10. 我的博客正式成立啦~