WAP之家:为您提供最全最新的WAP技术,CP.SP.3G等行业资讯。 WAP之家交流论坛全新开放 点击进入>>
WAP资讯 | 3G动态 | SP动态 | 运营商动态 | 内容商动态 | 制造商动态 | 论坛讨论>> 每次自动访问
WAP技术 | WAP源码 | 手机编程 | 手机源码 | 无线技术 | J2ME技术 | 手机软件 添加到收藏夹
IVR技术 | SP资料 | SMS MMS技术 | 商业方案 | IVR下载 | 书籍教程 | 工具软件 语言:繁體中文

WAP之家技术文章手机编程BlackBerryBlackBerry 应用程序开发者指南 第二卷:高级--第5章 BlackBerry浏览器

BlackBerry 应用程序开发者指南 第二卷:高级--第5章 BlackBerry浏览器
作者:佚名  来源:本站整理  发布时间:2008-3-15 1:07:24

/**

     * * Retrieves the pixels of width available for provided browser content.

     * */

    public int getAvailableWidth(BrowserContent browserField) {

       // Field has full screen.

       return Graphics.getScreenWidth();

       }

   

    /**

     * * Retrieves the history position for provided browser content.

     * */

    public int getHistoryPosition(BrowserContent browserField) {

       // No history support.

       return 0;

       }

    /**

     * * Retrieves cookies associated with a provided URL.

     * */

    public String getHTTPCookie(String url) {

       // No cookie support.

       return null;

       }

   

    /**

     * * Retrieves the specified resource.

     * */

    public HttpConnection getResource( RequestedResource resource, BrowserContent referrer)

    {

       if (resource == null) {

           return null;

           }

       // Check if this is cache-only request.

       if (resource.isCacheOnly()) {

           // No cache support.

           return null;

           }

       String url = resource.getUrl();

       if (url == null) {

           return null;

           }

      

       // If referrer is null, return the connection.

       if (referrer == null) {

           HttpConnection conn;

           try {

              return (HttpConnection) Connector.open(resource.getUrl());

              }

           catch (IOException e) {

              return null;

              }

           }

       else {

           // If referrer is provided, set up the connection on a

           // separate thread.

           Application.getApplication().invokeLater(

                  new RetrieveThread(resource, referrer));

           }

       return null;

       }

    /**

     * * Invokes the provided runnable object.

     * */

    public void invokeRunnable(Runnable runnable) {

(new Thread(runnable)).run();

}

}

class RenderingThread implements Runnable {

    BrowserContent _browserField;

    RenderingThread(BrowserContent field) {

       _browserField = field;

       }

    public void run() {

       try {

           browserField.finishLoading();

           }

       catch (RenderingException e) {

          

       }

    }

    }

 

class RetrieveThread implements Runnable {

    BrowserContent _browserField;

    RequestedResource _resource;

    RetrieveThread(RequestedResource resource, BrowserContent referrer) {

       _browserField = referrer;

       _resource = resource;

       }

    public void run() {

       HttpConnection conn;

       try {

           conn = (HttpConnection) Connector.open(_resource.getUrl());

           }

       catch (IOException e) {

           return;

       }

       _resource.setHttpConnection(conn);

       _browserField.resourceReady(_resource);

       }

}


支持附加MIME类型

浏览器页面APInet.rim.device.api.browser.plugin包中,它允许第三方应用程序向描绘库将它们自己注册为BlackBerry浏览器不支持的MIME类型的描绘提供者.
: 如果你试着注册一个BlackBerry浏览器已经支持的MIME类型,BrowserFieldProviderRegistry.register()方法会抛出一个异常.为得到支持的MIME类型列表,调用RenderingSession.getSupportedMimeType().

注册一个MIME类型的呈现提供者

为支持附加的MIME类型,需要扩展BrowserContentProvider抽象类.为了指定显示特性,例如没有滚动条显示或全屏显示,需要实现BrowserPageContext接口.

呈现的类库调用BrowserContentProvider.getAccept()BrowserContentProvider.getSupportedMimeTypes()标志提供者呈现的MIME类型.

 

列出接受的MIME类型

getAccept()getSupportedMimeTypes()的实现列出了提供者接受的MIME类型,它们给定了一组呈现的选项. getAccept()方法考虑到已经设置的呈现选项.这个例子假设没有设置呈现选项.为得到更多信息.参看API参考RenderingOptions

public String[] getAccept(RenderingOptions context) {

    // Return subset of getSupportedMimeTypes() if accept depends on rendering

    // options. For example, HTML can be turned off in the rendering options.

    // This would cause HTMLConverter to remove HTML MIME types.

    return ACCEPT;

    }

 

public String[] getSupportedMimeTypes() {

    return ACCEPT;

    }

指定显示特征

BrowserPageContext接口的实现指定了显示的特征.如果没有实现此接口,将使用缺省的值.

在本例中,所有属性都是整型.一个带有Boolean,String,Object属性的应用程序将实现对应的方法.

public boolean getPropertyWithBooleanValue(int id, boolean defaultValue) {

    // TODO Implement.

    return false;

    }

 

public int getPropertyWithIntValue(int id, int defaultValue) {

    if (id == BrowserPageContext.DISPLAY_STYLE) {

       // Turn off the scroll bar.

       return BrowserPageContext.STYLE_NO_VERTICAL_SCROLLBAR;

       }

    return 0;

}

 

public Object getPropertyWithObjectValue(int id, Object defaultValue) {

    // TODO Implement.

    return null;

    }

 

public String getPropertyWithStringValue(int id, String defaultValue) {

    // TODO Implement.

    return null;

}

 

获取一个域呈现内容

实现getBrowserContent(BrowserContentProviderContext)方法.

public BrowserContent getBrowserContent( BrowserContentProviderContext context)

                throws RenderingException {

    if (context == null) {

       throw new RenderingException("No Context is passed into Provider");

       }

    BrowserContentBaseImpl browserContentBaseImpl = new

    BrowserContentBaseImpl(context.getHttpConnection().getURL(), null,

           context.getRenderingApplication(),

           context.getRenderingSession().getRenderingOptions(), context.getFlags());

    VerticalFieldManager vfm = new VerticalFieldManager(Manager.VERTICAL_SCROLL);

    vfm.add(new LabelField("Mime type: ") );

    vfm.add(new LabelField(ACCEPT[0]));

    vfm.add(new SeparatorField());

    vfm.add(new LabelField("Content of the resource file: \n"));

    vfm.add(new SeparatorField());

    try {

       HttpConnection conn = context.getHttpConnection();

       InputStream in = conn.openInputStream();

       byte[] data = IOUtilities.streamToBytes(in);

       vfm.add(new LabelField(new String(data)));

       }

    catch (IOException e) {

       // TODO Implement.

       e.printStackTrace();

       }

   

    browserContentBaseImpl.setContent(vfm);

    browserContentBaseImpl.setTitle(ACCEPT[0]);

    // Set browser page context, this will tell the browser how to display this

    // field.

    browserContentBaseImpl.setBrowserPageContext(this);

    return browserContentBaseImpl;

    }

 

BlackBerry启动时注册

创建一个类库工程,将它的属性设置为启动时自动运行.libMain(),调用BrowserFieldProviderRegistry.getInstance()方法,然后再调用invoke register().

public static void libMain( String[] args ) {

    BrowserContentProviderRegistry converterRegistry =

       BrowserContentProviderRegistry.getInstance();

    if (converterRegistry != null) {

       converterRegistry.register(new BrowserPlugin());

       }

    }

代码实例

下面的例子包含2个文件, LoaderApp.javaBrowserPlugin.java.BlackBerry设备启动时LoaderApp.java增加一个新支持的MIME类型. BrowserPlugin.java包含了一个扩展了BrowserContentProvider的类.

: BrowserPlugin.javaLoaderApp.java

/**

* LoaderApp.java

* Copyright (C) 2004-2005 Research In Motion Limited.

*/

package com.rim.samples.docs.browser;

import net.rim.device.api.browser.plugin.BrowserContentProviderRegistry;

 

final class LoaderApp {

    public static void libMain( String[] args ) {

       BrowserContentProviderRegistry converterRegistry =

           BrowserContentProviderRegistry.getInstance();

       if (converterRegistry != null) {

           converterRegistry.register(new BrowserPlugin());

           }

       }

    }

 

/**

* BrowserPlugin.java

* Copyright (C) 2004-2005 Research In Motion Limited.

*/

package com.rim.samples.docs.browser;

import java.io.IOException;

import java.io.InputStream;

import javax.microedition.io.HttpConnection;

import net.rim.device.api.browser.field.*;

import net.rim.device.api.browser.plugin.*;

import net.rim.device.api.ui.Manager;

import net.rim.device.api.ui.component.LabelField;

import net.rim.device.api.ui.component.SeparatorField;

import net.rim.device.api.ui.container.VerticalFieldManager;

/**

* Create a file with xxtest extension and associate that type with

* application/x-vnd.rim.xxxtest mime type on any server.

*/

public final class BrowserPlugin extends BrowserContentProvider

                        implements BrowserPageContext {

    private static final String[] ACCEPT = {"application/x-vnd.rim.xxxtest"};

    /**

     * Retrieves list of mime types this provider can accept given a set of rendering options.

     *  * @param context Rendering options in place this provider should consider.

     *  * @return Array of mime types this provider will accept, given the provided rendering

     *