Java:共享增量变量

时间:2012-01-19 21:25:46

标签: java blackberry

我正在制作一个黑莓应用程序我需要在不同的入口点之间共享我的应用程序中的公共变量。该变量是一个简单的计数,用于跟踪图标主屏幕上的通知量。每次计划的后台应用程序更新时,它都会递增计数变量,然后由setValue(count)使用它来显示它。有人建议使用singleton和runtimestore方法。我正在寻找这种方法,并在论坛上找到了一个代码片段:

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

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

我的问题是我仍然不知道如何正确使用这段代码,我也看了

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

但我不确定如何在我的代码中实现它。我试图增加变量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);
        }
    }
}

使用RuntimeStore.getInstance()。put(GUID,countsIcon);在我的UserInterface类中创建错误可能是因为我在错误或不正确的位置使用它。我最近才开始使用黑莓开发和Java,所以这对我来说是非常新的。如果有任何帮助,我已附上我的代码的主要部分。

再次感谢您的帮助,我们将不胜感激!


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);
        }
    }
}

3 个答案:

答案 0 :(得分:0)

您只需要一个静态变量,但看起来您已经拥有了一个(iconCount)。如果它没有按照你想要的方式工作,你可能想尝试创建另一个跟踪带有静态变量的iconCount的类,该静态变量可以从你想要访问它的类中访问。

答案 1 :(得分:0)

通过RIM article on singletons阅读。正如他们所说,为每个进程初始化静态变量。使用Runtimestore确保所有进程都访问一个单例对象。

我打算在这里编辑代码,但是我更容易将它放在我的博客上: http://www.hrbuckley.net/2012/01/using-singleton-classes-in-blackberry.html

答案 2 :(得分:0)

只需创建一个类,将该变量放在该类中,并在要访问该变量的每个其他类中扩展该类。 修改任何类中的计数器变量的值,它将自动反映在其他类中。不要忘记声明它是静态的