Singleton 是否可以在 BlackBerry Java 中持久存储?

发布于 2025-01-03 04:37:05 字数 372 浏览 1 评论 0原文

我正在开发一个 Blackberry Java 应用程序,我需要应用程序中的更新计数,以便用户知道他们手机上有多少通知。通知数量显示在主屏幕图标旁边。我跟踪不同入口点之间的图标变量“iconCount”的方式是通过单例。即使用户打开和关闭设备后,我也需要保留此计数。因此,如果有 7 个可用更新,则在他们检查应用程序之前,将显示 7 个更新。当设备关闭时,它不会重置为 0。

作为一个测试,看看如果我将计数设置为 7 并通过模拟器关闭设备会发生什么;我按住电源按钮,直到显示“关闭设备,按任意键中止”,然后屏幕变黑。我等了几秒钟,再次按下挂断按钮,模拟器启动后显示 7 个新通知。

这对我来说有点奇怪,我想你需要持久存储来完成这个?我是否正确关闭了设备,或者即使在设备关闭后单例实际上仍保留该变量?

I am developing a Blackberry Java application and I need an update count in the app such that the user will know how many notifications they have on the phone. The number of notifications show up on the home screen beside the icon. The way I am keeping track of the icon variable "iconCount" between different entry points is through a singleton. I need to have this count be present even after the user turns off their device on and off. So if there are 7 updates available for them 7 will show up until they check the application. It will not reset to 0 when the device is turned off.

As a test to see what would happen if I set the count to 7 and turned off the device through the simulator; I held down the power button until it stated "turning device off, press any key to abort" then the screen went black. I waited a few seconds and pressed the hangup button again and the simulator powered up to show 7 new notifications.

This is a bit strange to me, I though you would need persistent store to accomplish this? Was I turning off the device properly, or does a singleton in fact hold the variable even after the device is shut off?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

画中仙 2025-01-10 04:37:05

以这种方式关闭设备并不会真正关闭它,这就是您的单例保留其价值的原因。如果您希望该值在重新启动或电池拔出后保持不变(这实际上会将其关闭),那么您必须将其放入持久存储中。

Turning the device off in that manner does not really turn it off, that is why your singleton retains its value. If you want the value to persist accross reboots or battery pulls (which really turns it off) then you will have to put it in the persistent store.

友谊不毕业 2025-01-10 04:37:05

静态成员(作为单例)在给定应用程序中将是唯一的。但由于 BlackBerry Java 实现非常“特殊”(至少可以这么说),对于每个备用入口点,您将有一个不同的“应用程序实例”,因此将创建多个单例实例。

如果您想在运行时在多个应用程序之间共享一个对象,则必须将其发布到 RuntimeStore 中。如果您还需要持久性,请在较新的操作系统中使用PersistentStore、纯文件或 sqlite 数据库。

现在关于模拟器:不要相信模拟器。一旦你关闭你的应用程序,你的变量就会消失。即使您有后台进程,当您关闭设备时,它也将不再存在。在真实设备上进行测试。

请记住:BlackBerry 不是 Java。您将看到许多违反 Java 规范的行为(例如,Persistable 接口不是继承的事实),因此请习惯它。

A static member (as the singleton) will be unique inside a given application. But because BlackBerry Java implementation is so "particular" (to say the least), for each alternate entry point, you'll have a different "application instance", so several instances of your singleton will be created.

If you want to share an object at runtime between several applications, you have to publish it in the RuntimeStore. If you also need persistence, then use PersistentStore, or plain files, or sqlite databases in newer OSes.

And now about the simulator: don't trust the simulator. Once you close your app your variables will vanish. Even if you have a background process, when you turn off the device it won't exist any more. Test on a real device.

Remember: BlackBerry is not Java. You'll see many violations of the Java Specification (like for instance, the fact that the Persistable interface is not inherited) so get used to it.

季末如歌 2025-01-10 04:37:05

单例设计模式中没有任何固有的东西会导致任何形式的持久性。也就是说,BlackBerry JVM 或您的代码可能正在执行一些特殊操作。没有代码示例,就不可能知道。

否则,当单例从内存中卸载(并且放在磁盘上)时,数据将消失。

可能的情况是,黑莓操作系统在关闭并在启动时恢复应用程序内存状态时本质上处于“休眠”状态。

There is nothing inherent in the Singleton design pattern that causes any sort of persistence. That said, it is possible that the BlackBerry JVM or your code is doing something special. Without a code sample, it is impossible to know.

Otherwise, when the singleton is unloaded from memory (and not put on disk), the data will evaporate.

The likely case is that the BlackBerry OS is essentially "hibernating" when it turns off and restores the application memory state on boot.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文