计算在Blackberry上加载网页的进度

时间:2011-11-01 14:50:44

标签: blackberry progress-bar

我想显示加载网页的进度。我使用BrowserFieldListener来计算网页的进度。但屏幕上没有显示任何内容。如何计算这一进度的百分比?

类BrowserFieldDemoScreen扩展了MainScreen {

public BrowserFieldDemoScreen()
{
    BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
    myBrowserFieldConfig.setProperty(BrowserFieldConfig
.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);


    BrowserField browserField = new BrowserField();



   browserField.requestContent("http://www.blackberry.com");
   browserField.addListener(new BrowserFieldListener() {

    public void documentAborted(BrowserField browserField, Document document) throws Exception
    {
        if ((browserField != null) && (document != null))
        {
            String title = browserField.getDocumentTitle();
            String url = document.getBaseURI();
            browserField.getErrorHandler().displayContentError(url, "Aborted loading document '" + title + "'");
        }
        super.documentAborted(browserField, document);
    }


    public void documentCreated(BrowserField browserField, ScriptEngine scriptEngine, Document document) throws Exception
    {
        if ((browserField != null) && (document != null))
        {
            String title = browserField.getDocumentTitle();
            String url = document.getBaseURI();
            System.out.println("[" + this.getClass().getName() + "] Document '" + title + "' created for URL " + url);
        }
        super.documentCreated(browserField, scriptEngine, document);
    }

    /**
     * 
     * @param browserField - The BrowserField to which the document belongs
         * @param document - The document for which this event belongs.
     */
    public void documentError(BrowserField browserField, Document document) throws Exception
    {
        if ((browserField != null) && (document != null))
        {
            String title = browserField.getDocumentTitle();
            String url = document.getBaseURI();
            browserField.getErrorHandler().displayContentError(url, "Error loading document '" + title + "'");
        }
        super.documentError(browserField, document);
    }


    public void documentLoaded(BrowserField browserField, Document document) throws Exception
    {
        if ((browserField != null) && (document != null))
        {
            String title = browserField.getDocumentTitle();
            System.out.println("[" + this.getClass().getName() + "] Document '" + title + "' loaded");
        }
        super.documentLoaded(browserField, document);
    }


    public void documentUnloading(BrowserField browserField, Document document) throws Exception
    {
        if ((browserField != null) && (document != null))
        {
            String title = browserField.getDocumentTitle();
            System.out.println("[" + this.getClass().getName() + "] Document '" + title + "' unloading");
        }
        super.documentUnloading(browserField, document);
    }


    public void downloadProgress(BrowserField browserField, ContentReadEvent event) throws Exception
    {
        if ((browserField != null) && (event != null))
        {
            String title = browserField.getDocumentTitle();
            double amtComplete = event.getItemsRead() / event.getItemsToRead();
            System.out.println("[" + this.getClass().getName() + "] Document '" + title + "' loading " + String.valueOf(amtComplete) + "% complete");
        }
        super.downloadProgress(browserField, event);
    }});


}

}

1 个答案:

答案 0 :(得分:0)

要计算当前在BrowserField中加载的网页的百分比,您应该将BrowserFieldListener附加到您的BrowserField。

您感兴趣的事件是downloadProgress(BrowserField browserField, ContentReadEvent event)

以下是来自官方Blackberry支持论坛的精彩教程,它将教您完全按照自己的意愿行事:BrowserField Sample Code