private static final String tag = "PhoneListenService";    // 电话管理者对象    private TelephonyManager mTelephonyManager;    // 电话状态监听者    private MyPhoneStateListener myPhoneStateListener;    // 动态监听去电的广播接收器    private InnerOutCallReceiver mInnerOutCallReceiver;

onCreate

mTelephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);        myPhoneStateListener = new MyPhoneStateListener();        mTelephonyManager.listen(myPhoneStateListener, PhoneStateListener.LISTEN_CALL_STATE);        // 动态注册广播接收器监听去电信息        mInnerOutCallReceiver = new InnerOutCallReceiver();        // 手机拨打电话时会发送:android.intent.action.NEW_OUTGOING_CALL的广播        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_NEW_OUTGOING_CALL);        registerReceiver(mInnerOutCallReceiver, intentFilter);
/**     * 动态注册广播接收器监听去电信息     */    class InnerOutCallReceiver extends BroadcastReceiver {        @Override        public void onReceive(Context context, Intent intent) {            // 获取播出的去电号码            String outPhone = getResultData();            Log.i(tag, "outPhone:" + outPhone);        }    }    /**     * 自定义内部类对来电的电话状态进行监听     */    class MyPhoneStateListener extends PhoneStateListener {        // 重写电话状态改变时触发的方法        @Override        public void onCallStateChanged(int state, String incomingNumber) {            super.onCallStateChanged(state, incomingNumber);            switch (state) {                case TelephonyManager.CALL_STATE_RINGING:                    Log.i(tag, "响铃:" + incomingNumber);                    System.out.print("响铃:" + incomingNumber);                    try {                        JSONObject obj = new JSONObject();                        obj.put("Interruption", "Begin");                        JavascriptRecordBridge.onInterruptionBegin(obj);                    } catch (JSONException e) {                        e.printStackTrace();                    }                    break;                case TelephonyManager.CALL_STATE_OFFHOOK:                    Log.i(tag, "接听");                    System.out.print("接听:");                    try {                        JSONObject obj = new JSONObject();                        obj.put("Interruption", "Begin");                        JavascriptRecordBridge.onInterruptionBegin(obj);                    } catch (JSONException e) {                        e.printStackTrace();                    }                    break;                case TelephonyManager.CALL_STATE_IDLE:                    Log.i(tag, "挂断");                    System.out.print("挂断:");                    try {                        JSONObject obj = new JSONObject();                        obj.put("Interruption", "End");                        JavascriptRecordBridge.onInterruptionBegin(obj);                    } catch (JSONException e) {                        e.printStackTrace();                    }                    break;            }        }    }

更多相关文章

  1. android中调用系统的打电话功能
  2. android 打电话程序的实现
  3. Android网络电话软件Sipdroid试用
  4. Android根据电话号码取得联系人姓名及头像
  5. 编写android拨打电话apk应用实例代码
  6. android-获取手机电话的状态
  7. 【android】 监听去电和来电电话状态
  8. android phone电话调用流程

随机推荐

  1. A-GPS定位与GPS定位的Android简单实现
  2. android 学习笔记1
  3. Android编译环境
  4. 搭建Android java开发环境 eclipse
  5. 关于Android的prelink(Linux)
  6. TextView英文自动换行解决方法
  7. android如何设置全屏模式
  8. Fedora 15下使用android ndk 编译ffmepg0
  9. Android添加背景音乐的方法
  10. Android自定义标题栏