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类型
浏览器页面API在net.rim.device.api.browser.plugin包中,它允许第三方应用程序向描绘库将它们自己注册为BlackBerry浏览器不支持的MIME类型的描绘提供者. 注册一个MIME类型的呈现提供者
为支持附加的MIME类型,需要扩展BrowserContentProvider抽象类.为了指定显示特性,例如没有滚动条显示或全屏显示,需要实现BrowserPageContext接口. 呈现的类库调用BrowserContentProvider.getAccept()和BrowserContentProvider.getSupportedMimeTypes()标志提供者呈现的MIME类型. 列出接受的MIME类型 getAccept()和getSupportedMimeTypes()的实现列出了提供者接受的MIME类型,它们给定了一组呈现的选项. getAccept()方法考虑到已经设置的呈现选项.这个例子假设没有设置呈现选项.为得到更多信息.参看API参考的RenderingOptions
指定显示特征 BrowserPageContext接口的实现指定了显示的特征.如果没有实现此接口,将使用缺省的值. 在本例中,所有属性都是整型.一个带有Boolean,String,和Object属性的应用程序将实现对应的方法.
获取一个域呈现内容 实现getBrowserContent(BrowserContentProviderContext)方法.
在BlackBerry启动时注册 创建一个类库工程,将它的属性设置为启动时自动运行.在libMain()里,调用BrowserFieldProviderRegistry.getInstance()方法,然后再调用invoke register().
代码实例
下面的例子包含2个文件, LoaderApp.java和BrowserPlugin.java.当BlackBerry设备启动时LoaderApp.java增加一个新支持的MIME类型. BrowserPlugin.java包含了一个扩展了BrowserContentProvider的类. 例: BrowserPlugin.java和LoaderApp.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 * |
