2016-10-04 99 views
2

onIncomingCall()是來自第三方庫pjsip中的類的重寫方法。當使用SIP進行來電時,將調用此方法。不知何故,如果呼叫應答代碼在同一方法內或在同一方法內調用,該方法可以使呼叫得到回答。但是我希望在用戶按下按鈕時可以接聽電話。我創建了一個回叫,並在呼叫來臨時讓用戶按下按鈕,但如果呼叫應答代碼在onIncomingCall()方法之外被呼叫,則該呼叫應答代碼不起作用。所以我決定把Thread.sleep(10000)放在onIncomingCall(),當用戶按下按鈕時,我想取消這個線程,這樣就可以執行呼叫應答代碼。我使用Thread.currentThread().interrupt(),但Thread.sleep沒有取消。我編寫了一個單獨的活動來測試這個功能,但它失敗了,這意味着Thread.currentThread.interrupt根本不適合我。實現這個目標的最佳選擇是什麼?請更新我..我真的很苦惱。Thread.currentThread()。interrupt()不適用於Android

@Override 
public void onIncomingCall(OnIncomingCallParam prm) { 

    onIncomingCallParam = prm; 

    try { 
     Thread.sleep(10000); 
    } catch(InterruptedException ie) { 
     ie.printStackTrace(); 
    } 

    answerCall(); 
} 

UPDATE:

我固定下面的方法

resetThread(); 
while (testThread) { 
    try { 
     Log.d(TAG,"testThread true"); 
     Thread.sleep(1000); 
    } catch (InterruptedException ie) { 
     ie.printStackTrace(); 
    } 

} 

Log.d(TAG,"Call Answering code"); 


private void resetThread() { 
     Thread newThread = new Thread(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        Thread.sleep(10000); 
        testThread = false; 
       } catch (InterruptedException ie) { 
        ie.printStackTrace(); 
       } 
      } 
     }); 
     try { 
      newThread.start(); 
     } catch (Exception ie) { 
      ie.printStackTrace(); 
     } 
    } 
+2

您必須從與正在休眠的線程(B)不同的線程(A)調用'interrupt()'。因此,從線程A開始,您需要調用'threadB.interrupt()' - 此時您正在中斷線程A,這沒有任何意義。 – assylias

+0

@assylias線程上調用Thread.sleep。如何區分線程A和線程B – praneel

+0

您可以在'onIncominCall()':'sleepingThread = Thread.currentThread();'中使用變量(可能需要變爲volatile),並在其他代碼中調用sleepingThread。中斷()'。但更有可能你應該改變你的設計 - 使用Thread.sleep()'確實是一種代碼味道。 – assylias

回答

0

也許所有呼叫必須被同一個線程上完成的,其創建的庫實例的問題。嘗試使用HandlerThread發佈消息,並在自定義處理程序中處理這些消息,而不是掛起線程。

+0

你能舉一些例子嗎? – praneel

+0

http://stackoverflow.com/questions/25094330/example-communicating-with-handlerthread –

2

這裏的問題是關係到一個事實,即你不要打斷右Thread,如果你打電話Thread.currentThread().interrupt(),你將中斷當前線程不是它目前正在睡覺的人。

這是一個明顯的例子,說明了最主要的想法:

// Here is the thread that will only sleep until it will be interrupted 
Thread t1 = new Thread(
    () -> { 
     try { 
      Thread.sleep(10_000L); 
      System.err.println("The Thread has not been interrupted"); 
     } catch (InterruptedException e) { 
      System.out.println("The Thread has been interrupted"); 
     } 
    } 
); 
// Start the thread 
t1.start(); 

// Make the current thread sleep for 1 sec 
Thread.sleep(1_000L); 
// Try to interrupt the sleeping thread with Thread.currentThread().interrupt() 
System.out.println("Trying to call Thread.currentThread().interrupt()"); 
Thread.currentThread().interrupt(); 
// Reset the flag to be able to make the current thread sleep again 
Thread.interrupted(); 

// Make the current thread sleep for 1 sec 
Thread.sleep(1_000L); 
// Try to interrupt the sleeping thread with t1.interrupt() 
System.out.println("Trying to call t1.interrupt()"); 
t1.interrupt(); 

輸出:

Trying to call Thread.currentThread().interrupt() 
Trying to call t1.interrupt() 
The Thread has been interrupted 

正如你可以在輸出中看到,當我們調用t1.interrupt()線程只能中斷換句話說,只有當我們中斷正確的Thread時。

+0

我實際上沒有做任何事情線程具體在這裏..我從字面上需要一種方法來打破該方法之間的代碼,直到用戶響應來電和一旦迴應,我需要繼續以相同的方法 – praneel

相關問題