拨打和挂断普通电话与紧急电话

紧急电话和普通电话使用的ACTION不一样。拨打前检查有无sim卡。

TelephonyManager manager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
        if(manager.getSimState() == TelephonyManager.SIM_STATE_READY){
            Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + "10086"));
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }else {
            
                    Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY, Uri.parse("tel:" + "112"));
                    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    startActivity(intent);
      
        }

紧急电话无法使用Itelephony 服务的api  endCall去挂断。可以使用ITelecomService 服务的api  endCall去挂断,即通过TelecomManager.endCall();

而至于这个两个服务有什么不同,没仔细看源码。

private void endPhone() {
        if(mIsIdle) return;
        try {
//            TelephonyManager tm = (TelephonyManager)getSystemService(Service.TELEPHONY_SERVICE);
//            ITelephony iTelephony;
//            Method getITelephonyMethod = TelephonyManager.class.getDeclaredMethod("getITelephony", (Class[]) null);
//            getITelephonyMethod.setAccessible(true);
//            iTelephony = (ITelephony) getITelephonyMethod.invoke(tm,
//                    (Object[]) null);
//            iTelephony.endCall();
            TelecomManager tm = (TelecomManager)getSystemService(Context.TELECOM_SERVICE);
            tm.endCall();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

猜你喜欢

转载自blog.csdn.net/b1480521874/article/details/81509416
今日推荐