2012-06-27 28 views
1

我正在通過使用以下代碼片段與android BroadCastReceiver捕獲android.intent.action.PHONE_STATE更改事件。我可以成功識別事件並處理來電也可以通過以下代碼完成。Android BroadCastReceiver和android.intent.action.PHONE_STATE事件

我的要求是確定來自特定號碼的呼叫,通過代碼結束該呼叫並針對該呼入事件調用Web服務。

在這裏,這段代碼運行兩次,用於呼叫接收和結束事件。

我想停止兩次調用此方法。我該如何停止爲第二個事件調用該代碼段(結束呼叫)。如果有人能幫助我,這將是非常棒的。

if (intent.getAction().equals(
      "android.intent.action.PHONE_STATE")) { 
     Log.i("INFO", "Call Received"); 

     try { 

      TelephonyManager telephony = (TelephonyManager) context 
        .getSystemService(Context.TELEPHONY_SERVICE); 

      Bundle bundle = intent.getExtras(); 

      Log.i("Calling database", "Creatinng DataHandler Object"); 
      DatabaseHandler db = new DatabaseHandler(context); 
      String phoneNumber =db.getContact().getPhoneNumber(); 

      Log.i("Retriving : ", "Retriving .."+ db.getContact().getPhoneNumber()); 


      if ((bundle.getString("incoming_number").equals(phoneNumber))) { 

       Log.i("Test Call", "This is the test call"); 

       @SuppressWarnings("rawtypes") 
       Class c = Class.forName(telephony.getClass().getName()); 
       Method m = c.getDeclaredMethod("getITelephony"); 
       m.setAccessible(true); 

       ITelephony telephonyService = (ITelephony) m 
         .invoke(telephony); 
       telephonyService = (ITelephony) m.invoke(telephony); 


       telephonyService.endCall(); 

       // Call the web service and send log details 

       ServiceClient sc = new ServiceClient(); 
       sc.serviceCall(context); 


      } else { 
       Log.i("Normal Call", "This is not the test call"); 
      } 

     } catch (Exception e) { 
      Log.i("Exception Occured", "Exception in call code snipet"); 
     } 
    } 

}

+0

哪部分代碼運行兩次? –

+0

你解決了問題 – Dory

回答

0

你可以嘗試儘快做這樣的事情作爲接收器開始:

Bundle bundle = nIntent.getExtras(); 
    String phoneNr= bundle.getString("incoming_number"); 

    if(null == phoneNr) 
    { // this is outgoing call so bail out 
     return; 
    } 
4

我相信您遇到的問題是,手機狀態變化兩次,一次是當手機響了,然後在通話結束後,手機閒置。

您只希望此代碼運行一次,因此最好的方法是添加電話狀態的附加檢查以查看它是否正在振鈴,如果電話正在振鈴,則結束呼叫。

這可以通過添加校驗以下來完成:

if (intent.getAction().equals(
     "android.intent.action.PHONE_STATE")) { 
    Log.i("INFO", "Call Received"); 

String state = intent.getStringExtra(TelephonyManager.EXTRA_STATE); 
if (state.equals(TelephonyManager.EXTRA_STATE_RINGING)) { 
//your code here that starts with try block 
} 

希望幫助

+0

這就是點兄弟,非常感謝 – Obtice

0

不能由於Android的安全理由終止使用第三方應用程序來電。檢查「Android應用程序開發食譜」頁碼「164」

0

我知道這太晚了,但我找到了這個麪糊的解決方案。 這通常發生在5.0和5.1上。你不能阻止它兩次打電話,但你可以有一個普遍的檢查,在一個點上做所有的工作。而且這種解決方案通用於所有操作系統。 下面是詳細信息檢查此link代碼

public void onReceive(Context context, Intent intent) { 
    Object obj = intent.getExtras().get("subscription"); 
     long subId; 
     if(obj == null) { 
     subId = Long.MIN_VALUE; // subscription not in extras 
     } else { 
     subId = Long.valueOf(obj.toString()); // subscription is long or int 
     } 

     if(subId < Integer.MAX_VALUE) { 
     // hurray, this is called only once on all operating system versions! 
     }} 

相關問題