public static void inAppDial(final BaseActivity activity, String dial) { if (TextUtils.isEmpty(dial)) { return; } final String phone = StringUtils.phoneFormat(dial); if (Constant.noCall.contains(phone)) { call(activity, phone); } else { HttpServer hServer = new HttpServer(Constant.URL.phoneCall, activity.getHandlerContext()); Map<String, String> headers = new HashMap<String, String>(); headers.put("sign", User.ACCESSKEY); hServer.setHeaders(headers); Map<String, String> params = new HashMap<String, String>(); params.put("accessid", User.ACCESSID); params.put("userTel", activity.getAppContext().currentUser().getPhone()); params.put("oppno", phone); hServer.setParams(params); hServer.get( new HttpRunnable() { @Override public void run(Response response) throws AppException { Map<String, String> info = response.getMapData("serverinfo"); RecordingContentView.isRefreshData = true; BaseContext.getSharedPreferences() .putString(Constant.Preferences.SP_CALL_DIAL, phone); call(activity, info.get("serverno")); activity.getRecentDaoImpl().insertCallLog(phone); } }); } }
@Override public void onCallStateChanged(int state, String incomingNumber) { switch (state) { case TelephonyManager.CALL_STATE_OFFHOOK: IDLE = true; if (myFV == null) { wm = (WindowManager) currentActivity.getAppContext().getSystemService(Context.WINDOW_SERVICE); View view = LayoutInflater.from(currentActivity.getAppContext()) .inflate(R.layout.module_diallistener, null); ImageView ivClose = (ImageView) view.findViewById(R.id.dial_listener_btn_close); ivClose.setOnClickListener( new View.OnClickListener() { @Override public void onClick(View v) { // 关闭悬浮窗口 if (wm != null && myFV != null) { wm.removeView(myFV); myFV = null; } } }); TextView tvMessage = (TextView) view.findViewById(R.id.dial_listener_text); tvMessage.setText("您正在与" + SP_CALL_DIAL + "录音通话中…"); myFV = new DialFloatView(currentActivity.getAppContext()); myFV.addView(view); // 设置LayoutParams(全局变量)相关参数 WindowManager.LayoutParams wmParams = myFV.getMywmParams(); // 设置window type wmParams.type = LayoutParams.TYPE_PHONE; // 设置图片格式,效果为背景透明 wmParams.format = PixelFormat.RGBA_8888; // 设置Window flag wmParams.flags = LayoutParams.FLAG_NOT_TOUCH_MODAL | LayoutParams.FLAG_NOT_FOCUSABLE; // 调整悬浮窗口至左上角 wmParams.gravity = Gravity.LEFT | Gravity.TOP; // 以屏幕左上角为原点,设置x、y初始值 wmParams.x = 0; wmParams.y = 0; // 设置悬浮窗口长宽数据 wmParams.width = android.view.ViewGroup.LayoutParams.MATCH_PARENT; wmParams.height = android.view.ViewGroup.LayoutParams.WRAP_CONTENT; // 显示myFloatView图像 wm.addView(myFV, wmParams); } break; case TelephonyManager.CALL_STATE_IDLE: // 拨打电话状态顺序1.CALL_STATE_IDLE 2.CALL_STATE_OFFHOOK // 3.CALL_STATE_IDLE由于只需要取最后一个CALL_STATE_IDLE所以在这里需要一个逻辑判断 if (IDLE) { IDLE = false; // 停止监听手机通话状态 if (manager != null) { manager.listen(stateListener, PhoneStateListener.LISTEN_NONE); } // 通话结束后关闭悬浮窗口 if (wm != null && myFV != null) { wm.removeView(myFV); myFV = null; } // 设置拨打的号码为空 BaseContext.getSharedPreferences() .putString(Constant.Preferences.SP_CALL_DIAL, AppConstant.EMPTYSTR); ActivityManager am = (ActivityManager) currentActivity.getSystemService(Context.ACTIVITY_SERVICE); ComponentName cn = am.getRunningTasks(1).get(0).topActivity; if (!cn.getClassName().equals(MainActivity.class.getName())) { // Android4.0以上系统默认打完电话后会跳转到 Intent intent = new Intent(currentActivity, MainActivity.class); intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); currentActivity.startActivity(intent); } } break; } }