BlackBerry 应用程序开发者指南 第二卷:高级--第13章 应用程序间共享运行时对象


作者:Confach 发表于 2006-04-29 20:30 pm
版权信息:可以任意转载, 转载时请务必以超链接形式标明文章原始出处 和作者信息.
http://www.cnblogs.com/confach/articles/388976.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 = 0x60ac754bc0867248L;

// 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( 0x60ac754bc0867248L, 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(0x60ac754bc0867248L);

}

catch(ControlledAccessException e) {

    // Handle exception.

}

获取一个未注册的运行时对象

>调用RuntimeStore.waitFor() 等待一个运行时对象注册.

RuntimeStore store = RuntimeStore.getRuntimeStore();

try {

    Object obj = store.waitFor(0x60ac754bc0867248L);

}

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日

 

posted @ 2006-04-29 20:30  张太国  阅读(1927)  评论(0编辑  收藏  举报