BlackBerry 应用程序开发者指南 第二卷:高级–第13章 应用程序间共享运行时对象
Confach 发表于 April 29, 2008 6:15 am
版权信息 :严禁转载, 若想推荐或收藏,请用链接的形式.
网址:http://www.inblackberry.com/web/development/docs/dev-guide-vol-2-share-objects-in-apps.html
13
第13章 应用程序间共享运行时对象
|
共享运行时对象 |
共享运行时对象
注: 当应用程序第一次访问运行时存储时,检查一个 NoClassDefFoundError.如果系统管理员通过应用程序控制限制访问运行时存储,将抛出此错误. 为获得更多信息,参看BlackBerry应用程序开发者指南 第2卷:高级 第1卷:基础.
BlackBerry设备使用一个运行时存储提供一个中心位置,在此位置上应用程序可以共享运行时对象.缺省的,仅由RIM数字签名的应用程序才可以访问运行时存储上的数据.联系RIM获得关于如何控制访问你的数据的信息.
获取运行时存储
>调用RuntimeStore.getRuntimeStore().
|
RuntimeStore store = RuntimeStore.getRuntimeStore(); |
为增加或获得运行时对象,调用RuntimeStore上的方法.
注:运行时存储不是持久的,如果BlackBerry重启,运行时存储的数据将丢失.
增加一个运行时对象
>调用RuntimeStore.put(long, String). 将一个唯一long ID和存储的对象作为参数.
|
RuntimeStore store = RuntimeStore.getRuntimeStore(); // Create an object and a unique number to identify the object. String msg = "Some shared text"; long ID = 0×60ac754bc0867248L; // put() throws an IllegalArgumentException if an object with the same ID exists. try { store.put( ID, msg ); } catch(IllegalArgumentException e) { // Handle exception - an object with the same ID exists. } |
替换一个运行时对象
>调用 replace().
|
RuntimeStore store = RuntimeStore.getRuntimeStore(); String newmsg = "Some new text"; try { // Returns the existing object with the specified ID if it exists; null // otherwise. Object obj = store.replace( 0×60ac754bc0867248L, newmsg); } catch(ControlledAccessException e) { // Handle exception - insufficient permissions. } |
获取一个注册的运行时对象
>调用RuntimeStore.get(). 将对象ID作为参数.
|
RuntimeStore store = RuntimeStore.getRuntimeStore(); // get() throws a ControlledAccessException if your application does not have read access to the specified object. try { // get() returns the objectm with the specified ID if it exists; null // otherwise. Object obj = store.get(0×60ac754bc0867248L); } catch(ControlledAccessException e) { // Handle exception. } |
获取一个未注册的运行时对象
>调用RuntimeStore.waitFor() 等待一个运行时对象注册.
|
RuntimeStore store = RuntimeStore.getRuntimeStore(); try { Object obj = store.waitFor(0×60ac754bc0867248L); } catch(ControlledAccessException e) { // Handle exception - insufficient permissions. } catch(RuntimeException e) { // Handle exception - time out. } |
注如果指定ID的对象不存在, waitFor()会阻止一个MAX_WAIT_MILLIS的最大数.如果此时对象还没有注册, waitFor()将会抛出一个RuntimeException异常.
Last Updated:2007年2月6日
