黑莓屏幕导航

时间:2012-03-30 13:54:02

标签: multithreading blackberry runnable invokelater

我有一个屏幕名称 DownloaderScreen ,当屏幕启动它将开始下载一些文件,当下载完成后,它将自动进入下一个屏幕。我使用以下代码。

public DownloaderScreen() {
        super(NO_VERTICAL_SCROLL | NO_HORIZONTAL_SCROLL | USE_ALL_HEIGHT
                | USE_ALL_WIDTH);

        this.application = UiApplication.getUiApplication();
        HorizontalFieldManager outerBlock = new HorizontalFieldManager(USE_ALL_HEIGHT);
        VerticalFieldManager innerBlock = new VerticalFieldManager(USE_ALL_WIDTH | FIELD_VCENTER);
        innerBlock.setPadding(0, 10, 0, 10);
        outerBlock.setBackground(BackgroundFactory
                .createBitmapBackground(LangValue.dlBgimg));
        outerBlock.add(innerBlock);
        add(outerBlock);

        phraseHelper = new PhraseHelper();
        final String[][] phraseList = phraseHelper.getDownloadList();

        gaugeField = new GaugeField("Downloading  ", 0, phraseList.length, 0, GaugeField.PERCENT); 
        innerBlock.add(gaugeField);

        Thread dlTread = new Thread() {
            public void run() {
                startDownload(phraseList);
            }
        };
        dlTread.start();

    }

private void startDownload(String[][] phraseList){

        if(phraseList.length!=0){

            for(int i=0; i < phraseList.length ; i++){//
               gaugeField.setValue(i);
               // code for download 
            }
        }
        goToNext();
    }

private void goToNext() {

final Screen currentScreen = application.getActiveScreen();  

    if (UiApplication.isEventDispatchThread()) {  
        application.popScreen(currentScreen);  
        application.pushScreen(new HomeScreen());  
        } else {  
            application.invokeLater(new Runnable() {  
                public void run() {  
                    application.popScreen(currentScreen);  
                    application.pushScreen(new HomeScreen());  
                }  
            });  
        }
}

代码工作正常并开始下载文件,当下载完成后,它将进入下一个屏幕。但是当没有要下载的文件时phraseList数组长度为零,它就不会继续下去。我的代码有什么问题?

2 个答案:

答案 0 :(得分:1)

将代码更改为

 if(phraseList.length!=0){

        for(int i=0; i < phraseList.length ; i++){//
           gaugeField.setValue(i);
           // code for download 
        }
        goToNext(); 

    }
    else{
         goToNext(); //if nothing to download, then it will goto the next screen.
        }

答案 1 :(得分:1)

GuageField不喜欢从0到0。当长度为零时,不要添加GuageField。