Android End等待呼叫以编程方式进行

如何解决Android End等待呼叫以编程方式进行

我正在为Call Center人员构建某种util应用程序。我必须在等待呼叫时执行操作的地方。

假设ABC试图打电话给我,我接了电话,而XYZ也试图打电话给我,那么XYZ呼叫将进入等待状态,直到我做出一些决定,例如用ABC结束呼叫或拒绝XYZ呼叫。

到目前为止,我能够检测到ABC在何时与我交谈,而XYZ打电话给我-我正在为同一事件进行活动。在这种情况下,如果我调用/运行disconnectCall方法,那么我的第一个电话将结束,但我想自动结束第二个电话,而不是第一个

在下面的代码中检查我的代码,该代码也可以检测到等待的呼叫 onNewIncomingCallReceived 。当将新呼叫置于等待状态时,将添加此日志。

public class CallListenerReceiver extends broadcastReceiver {

//The receiver will be recreated whenever android feels like it.  We need a static variable to remember data between instantiations
private static int lastState = TelephonyManager.CALL_STATE_IDLE;
private static Date callStartTime;
private static boolean isIncoming;
private boolean isUserOnCall = false;
private static final String TAG = "CallListenerReceiver";
private static String savednumber;  //because the passed incoming is only valid in ringing

@Override
public void onReceive(Context context,Intent intent) {

    //We listen to two intents.  The new outgoing call only tells us of an outgoing call.  We use it to get the number.
    if (intent.getAction().equals("android.intent.action.NEW_OUTGOING_CALL")) {
        savednumber = intent.getExtras().getString("android.intent.extra.PHONE_NUMBER");
    } else {
        String stateStr = intent.getExtras().getString(TelephonyManager.EXTRA_STATE);
        String number = intent.getExtras().getString(TelephonyManager.EXTRA_INCOMING_NUMBER);
        int state = 0;
        if (stateStr.equals(TelephonyManager.EXTRA_STATE_IDLE)) {
            state = TelephonyManager.CALL_STATE_IDLE;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_OFFHOOK)) {
            state = TelephonyManager.CALL_STATE_OFFHOOK;
        } else if (stateStr.equals(TelephonyManager.EXTRA_STATE_RINGING)) {
            state = TelephonyManager.CALL_STATE_RINGING;
        }

        onCallStateChanged(context,state,number);
    }
}

//Incoming call-  goes from IDLE to RINGING when it rings,to OFFHOOK when it's answered,to IDLE when its hung up
//Outgoing call-  goes from IDLE to OFFHOOK when it dials out,to IDLE when hung up

public void onCallStateChanged(Context context,int state,String number) {
    if (lastState == state) return;
    switch (state) {
        case TelephonyManager.CALL_STATE_RINGING:
            isIncoming = true;
            callStartTime = new Date();
            savednumber = number;

            if (lastState == TelephonyManager.CALL_STATE_OFFHOOK) {
                showToast(context,"New Call In Waiting");
                Log.d(TAG,"onNewIncomingCallReceived : ");
                disconnectCall(context);
            } else {
                showToast(context,"Call Received");
                Log.d(TAG,"onIncomingCallReceived : ");
            }
            break;

        case TelephonyManager.CALL_STATE_OFFHOOK:
            //Transition of ringing-> offhook are pickups of incoming calls.  nothing done on them
            if (lastState != TelephonyManager.CALL_STATE_RINGING) {
                isIncoming = false;
                callStartTime = new Date();
                Log.d(TAG,"onOutgoingCallStarted : ");
            } else {
                isUserOnCall = true;
                isIncoming = true;
                callStartTime = new Date();
                showToast(context,"Call Answered");
                Log.d(TAG,"onIncomingCallAnswered : ");
            }
            break;

        case TelephonyManager.CALL_STATE_IDLE:
            //Went to idle-  this is the end of a call.  What type depends on prevIoUs state(s)
            if (lastState == TelephonyManager.CALL_STATE_RINGING) {
                //Ring but no pickup-  a miss
                showToast(context,"onMissedCall");
                Log.d(TAG,"onMissedCall : ");
            } else if (isIncoming) {
                isUserOnCall = false;
                showToast(context,"onIncomingCallEnded");
                Log.d(TAG,"onIncomingCallEnded : ");
            } else {
                Log.d(TAG,"onOutgoingCallEnded : ");
            }
            break;
    }
    lastState = state;
}

private void disconnectCall(Context context) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
        try {
            TelecomManager telecomManager = (TelecomManager) context.getSystemService(Context.TELECOM_SERVICE);
            if (telecomManager != null)
                if (ActivityCompat.checkSelfPermission(context,Manifest.permission.ANSWER_PHONE_CALLS) == PackageManager.PERMISSION_GRANTED)
                    telecomManager.endCall();
                else
                    showToast(context,"Permission not available to disconnect call");
        } catch (Exception e) {
            try {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                if (tm == null) return;
                tm.getClass().getmethod("endCall").invoke(tm);
            } catch (Exception se) {
            }
        }
    } else endCall(context);
}

private void endCall(Context context) {
    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
    try {
        Method m = tm.getClass().getDeclaredMethod("getITelephony");
        m.setAccessible(true);
        com.android.internal.telephony.ITelephony telephonyService = (com.android.internal.telephony.ITelephony) m.invoke(tm);
        telephonyService.endCall();
    } catch (Exception e) {
        e.printstacktrace();
    }
}

private void showToast(Context context,String toastMessage) {
    Toast.makeText(context,toastMessage,Toast.LENGTH_LONG).show();
}

}

谁能帮助我自动结束第二个电话,那么用户已经在与其他人通话。我最近三天都在寻找解决方案,但没有得到。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?