简体   繁体   English

Java:共享增量变量

[英]Java: Sharing an Increment Variable

I am making a blackberry application I need to share a common variable in my application between different entry points. 我正在制作一个Blackberry应用程序,我需要在我的应用程序中的不同入口点之间共享一个公共变量。 The variable is a simple count which keeps track of the amount of notifications on the icon home screen. 该变量是一个简单的计数,用于跟踪图标主屏幕上的通知量。 Every time the scheduled background application updates it will increment the count variable which is then used by setValue(count) to display it. 每次计划的后台应用程序更新时,它将增加count变量,然后setValue(count)将其用于显示它。 Someone suggested to use the singleton and runtimestore approach. 有人建议使用单例和运行时存储方法。 I was searching around for this method and found a code snippet on a forum: 我正在四处寻找这种方法,并在一个论坛上找到了一个代码片段:

Integer i = new Integer(0);
RuntimeStore.getInstance().put(ID, i);
i.setValue(7);

//On other module:
Integer i = (Integer) RuntimeStore.getInstance().get(ID);

My issue is that I still do not know how to properly utilize this code, I have also looked at 我的问题是我仍然不知道如何正确利用此代码,我也查看了

http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp http://docs.blackberry.com/en/developers/deliverables/17952/CS_creating_a_singleton_by_using_rutnime_store_1554335_11.jsp

but I am not sure how to implement that in my code. 但是我不确定如何在我的代码中实现它。 I am trying to increment the variable iconCount which needs to be kept consistent amonst the background and foreground processes (ie if the user checks the app, the notification alerts will be reset to 0). 我正在尝试增加变量iconCount,该变量需要在后台进程和前台进程之间保持一致(即,如果用户检查了应用程序,则通知警报将重置为0)。

public void setVisible1(boolean visible, int count) {
    if (_indicator != null) {
        if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(++count);   //incrementing count
            UserInterface.iconCount++;      // also trying here
        } else {
            _indicator.setVisible(false);
        }
    }
}

Using RuntimeStore.getInstance().put(GUID, countsIcon); 使用RuntimeStore.getInstance()。put(GUID,countsIcon); in my UserInterface class creates errors probably because I am either using it wrong or not in the correct location. 在我的UserInterface类中创建错误可能是因为我使用错误或不在正确的位置使用它。 I've only recently started blackberry development and Java so this is very new to me. 我最近才刚开始黑莓开发和Java开发,所以这对我来说是非常新的。 I have attached the main portion of my code below if it is of any assistance. 如果有任何帮助,请在下面附上我代码的主要部分。

Again thanks for any help, it would be greatly appreciated! 再次感谢您的任何帮助,将不胜感激!


public class UserInterface extends UiApplication {
        static int iconCount;  //stores the value of the icon number
        public static void main(String[] args) {
        if (args != null && args.length>0 && "startVibrate".equals(args[0])){
            scheduleVibrate();
        }
        else{
            UserInterface theApp = new UserInterface();
            theApp.enterEventDispatcher();
        }
    }
    static MyAppIndicator SV = new MyAppIndicator();
    private static void scheduleVibrate()
    {
        SV.setVisible1(true,iconCount);
        Alert.startVibrate(2550); 
        ApplicationDescriptor current =     ApplicationDescriptor.currentApplicationDescriptor();
        current.setPowerOnBehavior(ApplicationDescriptor.DO_NOT_POWER_ON);
        ApplicationManager manger = ApplicationManager.getApplicationManager();
        manger.scheduleApplication(current,System.currentTimeMillis()+1000,true);
    }
    public UserInterface() {
        pushScreen(new UserInterfaceScreen());
    }
}


public class MyAppIndicator{

public ApplicationIndicator _indicator; 
public static MyAppIndicator _instance; 

MyAppIndicator () {
    setupIndicator();
}
public static MyAppIndicator getInstance() {
    if (_instance == null) {
        _instance = new MyAppIndicator ();
    }
    return(_instance);
}
public void setupIndicator() {

    //Setup notification 
    if (_indicator == null) {
        ApplicationIndicatorRegistry reg = ApplicationIndicatorRegistry.getInstance();
        _indicator = reg.getApplicationIndicator();

        if(_indicator == null) {
            ApplicationIcon icon = new ApplicationIcon(EncodedImage.getEncodedImageResource ("notificationsdemo_jde.png"));
            _indicator = reg.register(icon, false, true);  
            _indicator.setValue(0);
            _indicator.setVisible(false);
        }
    }
}

public void setVisible1(boolean visible, int count) {
    if (_indicator != null) {
         if (visible) {
            _indicator.setVisible(true);
            _indicator.setValue(++count);   //incrementing count
            UserInterface.iconCount++;      // also trying here
        } else {
            _indicator.setVisible(false);
        }
    }
}

A static variable is all you need, but it seems like you already have one(iconCount). 您只需要一个静态变量,但似乎已经有了一个(iconCount)。 If it isn't working the way you want, you might want to try creating another class that keeps track of the iconCount with a static variable that can be accessed from the classes you want to have access to it. 如果它不能按您想要的方式工作,则可能需要尝试创建另一个类,该类使用可以从您要访问它的类中访问的静态变量来跟踪iconCount。

Have a read throug the RIM article on singletons . 阅读有关RIM的RIM文章 As they say, static variables are initialized for each process. 正如他们所说,静态变量是为每个进程初始化的。 Use the Runtimestore to ensure that all processes access the one, singleton object. 使用运行时存储库以确保所有进程都访问一个单例对象。

I was going to edit the code into here, but it was easier for me to put it up on my blog: http://www.hrbuckley.net/2012/01/using-singleton-classes-in-blackberry.html 我打算将代码编辑到此处,但是对我来说,将其放在博客上更容易: http : //www.hrbuckley.net/2012/01/using-singleton-classes-in-blackberry.html

Simply create a class, put that variable in that class and extend this class in every other class where you want to access the variable. 只需创建一个类,将该变量放入该类中,然后将该类扩展到要访问该变量的所有其他类中。 Modify the value of the counter variable in any of the class it will automatically be reflected in other classes. 在任何类中修改计数器变量的值,它将自动反映在其他类中。 Don't forget to declare it static 不要忘记声明它为静态

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM