Android监听来电/挂断/接听
16lz
2022-05-31
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]在Avtivity中注册监听器
- vs xamarin android 监听返回键退出程序
- Android(安卓)结合WindowManager和WindowManager.LayoutParams显
- android 锁屏页
- Android中内容观察者的使用---- ContentObserver类详解
- android 自带的下拉刷新控件,含Demo(SwipeRefreshLayout控件)
- Android(安卓)Socket网络通信
- Android中系统设置参数改变监听(以时间同步为例)
- android sdcard 相关操作