2016-10-04 103 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

相关问题