0

我需要从我的应用程序向某个预定义的电话号码拨打电话,并且在一段时间间隔后,比如说在它开始响铃 10 秒后,我想自动终止这个呼叫,留下一个错误的电话。这可能吗?我使用以下代码开始通话

   try {
            Intent callIntent = new Intent(Intent.ACTION_CALL);
            callIntent.setData(Uri.parse("tel:"+phoneNumber);
            startActivity(callIntent);
        } catch (ActivityNotFoundException e) {
            Log.e("helloandroid dialing example", "Call failed", e);
        }

但我不知道如何停止这个特殊的电话。

4

2 回答 2

1

成功发起呼叫后。endCall()在您想要的时间间隔过去后,您始终可以使用它来断开它。

阅读此主题并在此站点上搜索类似主题: https ://stackoverflow.com/a/9987399/2021499

于 2013-03-20T11:31:07.400 回答
1

首先,您需要发起呼叫并监控其状态,然后在特定时间以编程方式结束呼叫。

你已经知道如何了initiate the call,我PhoneStateListenermonitor它的状态中添加了一个,并且我还添加了一个code snippet描述如何的end call programmatically

发起呼叫,

    private void establishCall()
{
            TelephonyManager tManager = (TelephonyManager) 
              getSystemService(Context.TELEPHONY_SERVICE);
    if (tManager .getSimState() != TelephonyManager.SIM_STATE_ABSENT)
    {                   

        try {
            Intent callIntent = new Intent(Intent.ACTION_CALL).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            callIntent.setData(Uri.parse("tel:"+phoneNumber));
            startActivity(callIntent);



            ListenToPhoneState listener = new ListenToPhoneState();
            tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);
        } catch (Exception e) {
            Log.e("Call failed", e.toString());
        }

    }
    else 
    {
        Toast.makeText(this, "Insert a Simcard into your device.", Toast.LENGTH_LONG).show();
    }
}

这是监听器的代码,

        private class ListenToPhoneState extends PhoneStateListener {

        boolean callEnded=false;
        public void onCallStateChanged(int state, String incomingNumber) {

            switch (state) {
            case TelephonyManager.CALL_STATE_IDLE:
                UTILS.Log_e("State changed: " , state+"Idle");

                break;
            case TelephonyManager.CALL_STATE_OFFHOOK:
                UTILS.Log_e("State changed: " , state+"Offhook");
                callEnded=true;
                break;
            case TelephonyManager.CALL_STATE_RINGING:
                UTILS.Log_e("State changed: " , state+"Ringing");

        //apply your logic here to wait for few seconds before calling killCall(this) function to end call


                break;


            default:
                break;
            }
        }

    }

这是结束当前通话的代码

public boolean killCall(Context context) {
try {
   // Get the boring old TelephonyManager
   TelephonyManager telephonyManager =
      (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);

   // Get the getITelephony() method
   Class classTelephony = Class.forName(telephonyManager.getClass().getName());
   Method methodGetITelephony = classTelephony.getDeclaredMethod("getITelephony");

   // Ignore that the method is supposed to be private
   methodGetITelephony.setAccessible(true);

   // Invoke getITelephony() to get the ITelephony interface
   Object telephonyInterface = methodGetITelephony.invoke(telephonyManager);

   // Get the endCall method from ITelephony
   Class telephonyInterfaceClass =  
       Class.forName(telephonyInterface.getClass().getName());
   Method methodEndCall = telephonyInterfaceClass.getDeclaredMethod("endCall");


       // Invoke endCall()
       methodEndCall.invoke(telephonyInterface);

   } catch (Exception ex) { // Many things can go wrong with reflection calls
      Logger.e("PhoneStateReceiver **" + ex.toString());
      return false;
   }
   return true;
}

在清单内需要以下权限,

<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.CALL_PHONE" />

我希望它会有所帮助!

于 2013-03-20T12:18:33.443 回答