开发者

How do I access global variables from a PhoneListener event?

开发者 https://www.devze.com 2023-02-20 11:06 出处:网络
I\'m developing an app for Blackberry on OS 5. I\'m trying to have some previously initialized variables accessed when a call is successfully made. However, the variables don\'t hold their value and I

I'm developing an app for Blackberry on OS 5. I'm trying to have some previously initialized variables accessed when a call is successfully made. However, the variables don't hold their value and I can't find why.

When the app gets started, testVariable is set to 5, everything works as expected. However, when I try to access the variable from the callConnected event, it is back at 0. Even if I put the variable inside the PhoneEvents class it still goes back to 0. It has one value for PhoneEvents and another for the screen. Shouldn't these variable have only one value given that it's not an instance variable? What am I doing wrong? Is there any other way to have a variable accessed from the PhoneEvents class and the TestScreen class and having it keep its value? Thanks in advance.

    public class TestApp extends UiApplication {
        public TestApp() {
            TestScreen screen = new TestScreen();
            UiApplication.getUiApplication().pushScreen(screen);
        }

        public static void main(String[] args) {
            Phone.addPhoneListener(new PhoneEvents());
            TestApp app = new TestApp();
            app.enterEventDispatcher();
        }
    }

    // The main screen.
    public class TestScreen extends MainScreen {
        public TestScreen() {
            this.setTitle("Test");
            GlobalClass.testVariable = 5;
        }
    }

    // A public static holding a variable I want to have access to.
    public class GlobalClass {
        public static int testVariable;
开发者_C百科    }

    // The method that gets called when a call is made.
    public class PhoneEvents implements PhoneListener {
        public void callConnected(int callId) {
            int x = GlobalClass.testVariable;
        }
    }


PhoneListener callbacks run in a different process. You notice this for example by calling UiEngine.getUiEngine().getActiveScreen(), which returns phone screen when you call it from PhoneListener callbacks and application screen when you call it before registering PhoneListener. Also static variables are different.

One solution is to put your application data to the RuntimeStore:

class ApplicationData
{
    public int testVariable;
}

ApplicationData appData = new ApplicationData();
appData.testVariable = 5;

RuntimeStore.getRuntimeStore().put(MY_DATA_ID, appData);

public class PhoneEvents implements PhoneListener {
    public void callConnected(int callId) {
        ApplicationData appData = (ApplicationData) RuntimeStore.getRuntimeStore().get(MY_DATA_ID);
        int x = appData.testVariable;
    }
}


This is just an alternate way to do it. Make the GlobalClass a Singleton. This will guarantee that you will get the same instance of the class every time.

Here is a link which shows how to make singletons.

Singleton with Arguments in Java

This Wikipedia link will give an in depth explanation about Singletons. Hope this helps

http://en.wikipedia.org/wiki/Singleton_pattern

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号