2013-03-20 37 views
0

我需要從我的應用程序打電話給某個預定義的電話號碼,並且經過一段時間間隔後,讓我們說10秒後鈴聲響起,我想自動終止此呼叫,一個錯誤的電話。這可能嗎? 我用下面的代碼來啓動電話android在我的應用程序中發出錯誤呼叫

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

,但我不知道如何制止這種特定的呼叫。

+1

做一些谷歌搜索_android電話manager_。 – mah 2013-03-20 11:23:32

回答

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" /> 

我希望它會有幫助!

+0

嗨,thx的代碼,我試過了,但不幸的是它不工作,聽衆從來沒有被解僱,有任何想法爲什麼? – user173488 2013-03-25 13:59:53

+0

調試後我發現問題是發送給監聽器的狀態,它始終爲0!當狀態改變爲call_state_ringing時,onstatechanged不會被調用... – user173488 2013-03-25 14:42:52

相關問題