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追加PhoneStateListenerし、方法を説明するmonitorも追加しました。code snippetend 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 に答える