需要权限:

<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;    @Override    public 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;        @Override    public 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";        @Override    public 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{        @Override        public 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);        }    }            }

转载http://stephen830.iteye.com/blog/1181010

更多相关文章

  1. 使用程序创建Android桌面快捷方式
  2. Android——ImageButton【图片按钮】的点击事件与属性
  3. Android拨打电话权限总结
  4. android打电话和发短信
  5. Android调用拨号按钮CALL_BUTTON
  6. Android通过http协议POST传输方式
  7. Android挂断电话流程

随机推荐

  1. Android获取系统cpu信息,内存,版本,电量等信
  2. Android(安卓)- Vibrator及VibrationEffe
  3. Bip44确定性算法的android实现
  4. [PX3][Android7.1]学习笔记 --- init.rc
  5. PhoneGap(Android)环境搭建教程
  6. Android图形显示系统——上层显示2:硬件加
  7. TextSurface实现文字呈现效果
  8. android文件存储的五种方式简介
  9. Android(安卓)Studio 设备模拟器(virtual
  10. Android(安卓)Service组件的生命周期及用