android从我的应用程序拨打未接来电

时间:2013-03-20 11:15:29

标签: android phone-call

我需要从我的应用程序拨打一个预定义的电话号码,并且在一段时间间隔后,让我们说它开始振铃后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);
        }

但我不知道如何停止此特定电话。

2 个答案:

答案 0 :(得分:1)

成功启动呼叫后。在所需的时间间隔过后,您可以随时使用endCall()来断开连接。

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

答案 1 :(得分:1)

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

您已经知道如何initiate the call,我已将PhoneStateListener添加到monitor其州,我还添加了code snippet,其中介绍了如何{{1} }}

发起通话,

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;
}

我希望它会有所帮助!!