Tuesday, July 16, 2013

Another Pesky issue

Been working on monitoring 'Connection Lost' and having opted to go with 'Heartbeat' method( http://stackoverflow.com/a/486695/1048839 ), I stumbled upon a pesky problem that was stubborn enough to have Mawuli convince me to reboot my laptop(1st time in 2 months). Even then, the problem persisted.

To handle new app updates, the app listens for updated from the server and acts accordingly.



  • public void handleNotify(JSONObject json) throws JSONException{      
            JSONObject json_obj = json.getJSONObject("result");        //Check for update variable        if(json_obj.getString("type").equals("update")){System.out.println("Update Found for..."+app_version);            String[] latest_version = split(json_obj.getString("ver")".");            String[] current_version = split(app_version, ".");          
                //Assign new verion number to app_version variable to avoid future            //checks during current session            app_version = json_obj.getString("ver");            //Compare versions and enable update download            if(!latest_version[0].equals(current_version[0])){                //Force download update                cmd_update = new Command("Update", Command.OK0);                cmd_exit = new Command("Exit", Command.EXIT1);                Alert alert = new Alert("Update""New Update available.\nPlease Download"null, AlertType.CONFIRMATION);                alert.addCommand(cmd_update);                alert.addCommand(cmd_exit);                alert.setCommandListener(this);             
                    Display.getDisplay(this).setCurrent(alert);             
                    //Sleep midlet                try {                  
                        Thread.sleep(100000);                    //Force App close                    instance.notifyDestroyed();                } catch (InterruptedException ex) {                    ex.printStackTrace();                }            }else if(!latest_version[1].equals(current_version[1])){                //Ask to update                cmd_update = new Command("Update", Command.OK0);              

                    Alert alert = new Alert("Update""New Update available.\nPlease Download"null, AlertType.CONFIRMATION);                alert.addCommand(cmd_update);              
                    alert.setCommandListener(this);             
                    Display.getDisplay(this).setCurrent(alert);                             
                    //Sleep midlet                try {                    for(int i=10; i>0; i--){                      
                            alert.setString("["+Integer.toString(i) +"] "+"New Update available.\nPlease Download");                        Thread.sleep(1000);                    } 
                     
                    } catch (InterruptedException ex) {                    ex.printStackTrace();                }            }else if(!latest_version[2].equals(current_version[2])){                //Ask to update                cmd_update = new Command("Update", Command.OK0);              

                    Alert alert = new Alert("Update""New Update available.\nPlease Download"null, AlertType.CONFIRMATION);              
                    alert.addCommand(cmd_update);             
                    alert.setCommandListener(this);             
                    Display.getDisplay(this).setCurrent(alert);             
                    //Sleep midlet                try {                    for(int i=10; i>0; i--){                      
                            alert.setString("["+Integer.toString(i) +"] "+"New Update available.\nPlease Download");                        Thread.sleep(1000);                    }                                    
                    } catch (InterruptedException ex) {                    ex.printStackTrace();                }            }          
             
            }     
         
        }//--End of handleNotify(JSONObject);
  • The problems occurs in the small sleeping thread for the last 2 options as it counts from 10 down to one. On the 3rd count(i.e no.7), it fires the cmd_update by itself and therefore tries to execute an update. The solution, weirdly enough, I found was to add another command to the alert and that stopped cmd_update from firing.

    //Ask to update
                    cmd_update = new Command("Update", Command.OK, 0);
                    cmd_exit = new Command("Exit", Command.EXIT, 1);
                    Alert alert = new Alert("Update", "New Update available.\nPlease Download", null, AlertType.CONFIRMATION);              
                    alert.addCommand(cmd_update);
                    alert.addCommand(cmd_exit);
                   
                    alert.setCommandListener(this);
                   
                    Display.getDisplay(this).setCurrent(alert);

    No comments:

    Post a Comment