2012-01-11 103 views
0

我正在开发一个使用网络功能的黑莓应用程序。该请求是通过按下一个按钮,用下面的代码执行:网络线程阻塞用户界面

mainButton=new BitmapButton(Bitmap.getBitmapResource("elcomercio.​gif"), Bitmap.getBitmapResource("elcomercio.gif"), Field.FOCUSABLE){ 
     protected boolean navigationClick(int status,int time) { 
      //It automatically add itself to the screen stack 
      waitScreen=new WaitScreen(); 
      TimeOutThread timeOut=new TimeOutThread(HomeScreen.this, 10000); 
      HttpHelper helper=new HttpHelper("http://www.elcomercio.com/rss/latest", null, HomeScreen.this, HttpHelper.GET); 
      UiApplication.getUiApplication().invokeLater(timeO​ut); 
      UiApplication.getUiApplication().invokeLater(helpe​r); 
      return true; 
     } 
    }; 

正如你可以看到TimeOutThread和HttpHelper无论从主题继承,使他们能够执行的主流外部调用。同样他们都会将当前屏幕作为委托对象接收,以便我可以在屏幕上稍后执行方法。在这种情况下,超时执行以下功能。

public void onTimeout() { 
    if(!didTimeout.booleanValue()){ 
     UiApplication.getUiApplication().popScreen(waitScr​een); 
     didTimeout=Boolean.TRUE; 
    } 
} 

超时方法被称为成功......甚至waitScreen弹出成功,并显示最后一个屏幕。但用户界面在那个时刻挂起......就像我的HttpThread仍在执行阻止用户界面......我知道这是因为当网络线程超时时......用户界面再次响应。我做错了什么?

回答

1

UiApplication.invokeLater()不在自己的执行线程中运行Thread对象。它在主事件派发线程中运行该对象 - 与运行UI的线程相同。您需要使用Thread.start()方法而不是UiApplication.invokeLater()方法,例如:

mainButton = new BitmapButton(Bitmap.getBitmapResource("elcomercio.​gif"), Bitmap.getBitmapResource("elcomercio.gif"), Field.FOCUSABLE) 
{ 
    protected boolean navigationClick(int status,int time) 
    { 
     waitScreen = new WaitScreen(); 
     TimeOutThread timeOut=new TimeOutThread(HomeScreen.this, 10000); 
     HttpHelper helper = new HttpHelper("http://www.elcomercio.com/rss/latest", null, HomeScreen.this, HttpHelper.GET); 
     timeO​ut.start(); 
     helpe​r.start(); 
     return true; 
    } 
};