Search This Blog

Tuesday, 8 May 2012

How to save a variable in Persistent store at particular address.

Hello friends ,
            Today i'll tell you how to use a persistent store to store a variable,it is helpful when your app needs one time activation and you want your app know automatically that if the device is registered or not , in that case what we do is we send the data to the server by using JSON for the registration and get some value in return as an acknowledgement for successful registration now if you store that value in persistent store and let your app check that variable everytime it statrt up.That makes your app capable of knowing that if the device is registered or not,now the issue comes that how to use persistent store.The very first step is to get the address of some register on device.So below is the process....

1.First type something in eclipse like-abc,urname,ur application name etc.
2.Now select the word you just typed and right click on it and select Convert String to long.You will get the address.
3.Now see the below code :

Code:

To check the Value stored in Persistent store:

PersistentObject _store;
_store = PersistentStore.getPersistentObject(0x1700827a621f6962L);//Use your address here.
       
        UiApplication.getUiApplication().invokeLater(new Runnable(){

            public void run() {
                synchronized(MyApp._store){
                    i = (String)MyApp._store.getContents();
                }
                if(i!=null){
                    Do something when register has a value.
//                                    }else{
                    Do something when register is empty
                }
            }
           
        });

To insert the value in Persistent Store:

try{
                                    synchronized(MyApp._store){
                                        MyApp._store.setContents(pass the value here);
                                        MyApp._store.commit();
                                        Dialog.alert("Successfully Activated!");
                                    }
                                    }catch(Exception e){
                                        Dialog.alert("Error : "+e.toString());
                                    }

Remeber you need to initialize the Persistent object first whether to insert the value or to check it , the best way to do this is declare and initialize the variable ,as here it is _store.....in MyApp java file and make it static

No comments:

Post a Comment