Android监听来电/挂断/接听
16lz
2021-01-23
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; } } }
更多相关文章
- android中调用系统的打电话功能
- android 打电话程序的实现
- Android网络电话软件Sipdroid试用
- Android根据电话号码取得联系人姓名及头像
- 编写android拨打电话apk应用实例代码
- android-获取手机电话的状态
- 【android】 监听去电和来电电话状态
- android phone电话调用流程