PleaseWaitPopupScreen

Hello, I use the code edition http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...

I my case, I have a class of service that calls the connecction and Treaty the info leave in a vector.

My code is:

FieldChangeListener listener = new FieldChangeListener() {
   public void fieldChanged(Field field, int context) {
    ServiceHelper serviceHelper = new ServiceHelper();
        Vector result = serviceHelper.getProductList();
     Dialog.alert("Hello There");
     if (result.isEmpty()) {
        Dialog.alert("No data found");
     } else {
       String jsonString = (String) result.elementAt(0);
        Dialog.alert(jsonString);
     }
}

In the ServiceHelper class that I have:

public Vector getProductList() {
  ConnectionThread ct = new ConnectionThread(
    Constants.ACTION_GET_PRODUCT_LIST);

  PleaseWaitPopupScreen.showScreenAndWait(ct, "Getting product list...");
  Vector responseVector = new Vector();
  if (ct.getResponseCode() == HttpConnection.HTTP_OK) {
    responseVector = ct.getResponseVector();
    Dialog.alert((String) responseVector.elementAt(0));
  }
  return responseVector;
}

But the PleaseWaitPopupScreen don't wait! It not immediately show the message "No data found" and on the PleaseWaiitScreen...

Any clue?

In fact, I think my article should be updated.  I think that the way you tried to use it, (i.e. citing as a blocking method), would in fact make it much easier to use. .

In any case, I want you to try something.  Currently, the showScreenAndWait (.) method is:

    public static void showScreenAndWait(final Runnable runThis, String text) {
        final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                // Now run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().popScreen(thisScreen);
                    }
                });
            }
        };
        threadToRun.start();
    }

Please change the following:

    public static void showScreenAndWait(final Runnable runThis, String text) {
        final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
        /*
        Thread threadToRun = new Thread() {
            public void run() {
                // First, display this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().pushScreen(thisScreen);
                    }
                });
                */
        Thread threadToRun = new Thread() {
            public void run() {
                // Run the code that must be executed in the Background
                try {
                    runThis.run();
                } catch (Throwable t) {
                    t.printStackTrace();
                    throw new RuntimeException("Exception detected while waiting: " + t.toString());
                }
                while ( !thisScreen.isDisplayed() ) {
                    try {
                       Thread.sleep(500);
                    } catch (Exception e) {
                    }
                }
                // Now dismiss this screen
                UiApplication.getUiApplication().invokeLater(new Runnable() {
                    public void run() {
                        UiApplication.getUiApplication().popScreen(thisScreen);
                    }
                });
            }
        };
        threadToRun.start();
        UiApplication.getUiApplication().pushModalScreen(thisScreen);
    }

This means that the code is actually wait in showScreenAndWait (.).  I did not code this way initially because it's a little more complicated to explain. But I think that it is easier to use 100%.  According to me, a little more explanation is a small price to pay for ease of use.

In any case if it works for you, then I will update the articles.

Tags: BlackBerry Developers

Similar Questions

  • PleaseWaitPopupScreen - more flexible approach?

    Hello

    I found a good tutorial by Peter Strange located here on the creation of a pop-up screen.

    I had a question about the implementation of this in a real-world example, I need my unique PleaseWaitPopupScreen to contain a class derived from network. If I look at the the PleaseWaitPopupScreen show() method I see that it creates the network connection then assigned the new connection as a member variable. But what happens if I have a class derived from networking (my wireless network being derived from the abstract class).

    Could you please clarify how I could use a class derived from networking with such an approach? Please let me know I've been running in circles for hours.

    Thank you

    Eric

    You are right, and this is noted in the comments to this article.  To find these, scroll down, you will see a Comments link - there are 4 comments atm.  Please see the comments.  However, they direct you here:

    http://supportforums.BlackBerry.com/T5/Java-development/PleaseWaitPopupScreen/m-p/544983#M110325

    It is a version of the same code block.

  • How to start a PopupScreen?

    Hello

    I am facing a strange problem with PopupScreen and how to throw one.

    I try to make a page "load" that is displayed during the execution of a thread.

    I started with a sample code here: http://supportforums.blackberry.com/t5/Java-Development/Sample-Please-Wait-or-Progress-Bar-Code/td-p...

    That's why the way I call the PopupScreen can be weird.

    My problem is that when I want to launch the anti-pop in the constructor of the screen, I have a RuntimeException and when I put it in a ButtonField change event and simulate the click event it works fine.

    So I was wondering what is the difference?

    For your information, this is my code that works:

    public final class ScreenCache extends MainScreen
    {
        Screen s;
        PleaseWaitPopupScreen waitScreen = null;
    
        public ScreenCache()
        {
            super(NO_VERTICAL_SCROLL | USE_ALL_WIDTH);
            s = this;
    
            ButtonField startButton = new ButtonField("Start", ButtonField.LEFT | ButtonField.CONSUME_CLICK);
            startButton.setChangeListener( new FieldChangeListener()
            {
                public void fieldChanged(Field field, int context)
                {
                    waitScreen = new PleaseWaitPopupScreen("Chargement", "Chargement des données");
    
                    ThreadCache _requestThread = new ThreadCache(waitScreen);
                    _requestThread.start();
    
                    UiApplication.getUiApplication().pushModalScreen(waitScreen);
    
                    UiApplication.getUiApplication().popScreen(s);
                    UiApplication.getUiApplication().pushScreen(new ScreenList());
                }
            });
            this.add(startButton);
    
            EventInjector.invokeEvent(new EventInjector.NavigationEvent(EventInjector.NavigationEvent.NAVIGATION_CLICK, 0, 0, 0));
            EventInjector.invokeEvent(new EventInjector.NavigationEvent(EventInjector.NavigationEvent.NAVIGATION_UNCLICK, 0, 0, 0));
        }
    
       protected void paint(Graphics graphics)
        {
            EncodedImage ei = EncodedImage.getEncodedImageResource("img/SplashScreen.png");
            EncodedImage ei2 = new ToolsImage().ResizeImage(ei, Display.getWidth(), 0);
    
            graphics.drawBitmap(new XYRect(0, 0, Display.getWidth(), Display.getHeight()), ei2.getBitmap(), 0, 0);
        }
    
       protected boolean onSavePrompt()
       {
           return true;
       }
    
    }
    

    My PopupScreen is very simple, it only shows text and animated images, so the problem probably isn't there.

    I'm on version 5.0 and I'm on the BlackBerry 9550 Simulator tetsing.

    Try using invokeLater pushModal, anti-pop and pushScreen calls.

    Scott

  • Updade UI thread

    Hello

    I want to show a list of message on a scree. I have to get a web service messages, showing a popUpScreen with a message waiting, while the service is invocated.

    To do that I have created a RespuestaServicio interface and three classes, MyScreen where I want to display messages, it implements RespuestaServicio methods that are called after the Web service response is obtained, PleaseWaitPopupScreen extends from PopupScreen, which has a method that receives a thread to run while the popupscreen is showed and close itself when the thread end and finally

    LlamadaWSThread is passed to the PleaseWaitPopupScreen call and that the webservice to get the list of messages.

    It's code summarized:

    public class MyScreen extends MainScreen implements RespuestaServicio{
    
        private VerticalFieldManager listaMensajes;
    
        public MyScreen() {
            super(Manager.NO_VERTICAL_SCROLL);
            HorizonlatFieldManager fondo = new HorizonlatFieldManager();
            listaMensajes = new VerticalFieldManager();
            vertical.add(listaMensajes);
            fondo.add(vertical);
            this.add(fondo);
            buscarMensajes();
        }
    
        public void buscarMensajes(){
        // Here I get some parameters like url an xml to pass to LlamadaWSThread constructor
           LlamadaWSThread serviceCaller = new LlamadaWSThread(url, this, xml);
                  PleaseWaitPopupScreen.showScreenAndWait(serviceCaller);
        }
    
        private void llenarLista(String[] campos){
            LabelField campo;
            for(int i = 0; i < campos.length; i++){
                campo = new LabelField(campos[i]);
                campo.setMargin(0, 10, 0, 10);
                listaMensajes.add(campo);
            }
        }
    
        public void requestFailed(final String message) {
           UiApplication.getUiApplication().invokeLater(new Runnable() {
                     public void run() {
                    Dialog.alert("requestFailed: "+message);
                     }
                  });
        }
    
        public void requestSucceeded(Document result) {
            String[] mensajes;
            // Here I proccess result to get mesajes...;
            llenarLista(campos);
        }
    }
    
    public class PleaseWaitPopupScreen extends PopupScreen {
    
        private static LabelField _ourLabelField = null;
    
        private PleaseWaitPopupScreen() {
            super(new VerticalFieldManager());
            ourLabelField = new LabelField("Please wait");
            this.add(urLabelField);
        }
    
        public static void showScreenAndWait(final Runnable runThis) {
            final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(text);
            Thread threadToRun = new Thread() {
                public void run() {
    
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            UiApplication.getUiApplication().pushScreen(thisScreen);
                        }
                    });
    
                    try {
                        runThis.run();
                    } catch (Exception t) {
    
                    }
    
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            if(thisScreen != null){
                                try {
                   UiApplication.getUiApplication().popScreen(thisScreen);
                } catch (Exception e) {
                   Dialog.alert(e.getMessage());
                      }
                            }
                        }
                    });
                }
            };
            threadToRun.start();
        }
    }
    
    public class LlamadaWSThread extends Thread {
        private RespuestaServicio pantalla;
        private ConstructorXml xml;
        private String url;
    
        public LlamadaWSThread(String url, RespuestaServicio pantalla, ConstructorXml xml) {
            super();
            this.url = url;
            this.pantalla = pantalla;
            this.xml = xml;
        }
    
        public  void    run()
        {
            HttpConnection  conn = null;
            OutputStream    oStream = null;
            try
            {
                conn    =   (HttpConnection)Connector.open( url );
                conn.setRequestMethod( HttpConnection.POST );
                oStream =   conn.openOutputStream();
                oStream.write( xml.getXml().getBytes() );
                oStream.flush();
    
                      InputStream is = conn.openInputStream();
                byte [] response = IOUtilities.streamToBytes(is);
                is.close();
                ByteArrayOutputStream   baos  =  new ByteArrayOutputStream();
                baos.write( response );
                DocumentBuilderFactory  dbf =   DocumentBuilderFactory.newInstance();
                DocumentBuilder db  =   dbf.newDocumentBuilder();
                Document    doc =   db.parse( new ByteArrayInputStream( baos.toByteArray() ) );
                pantalla.requestSucceeded(doc);
            }
            catch (Exception except)
            {
                pantalla.requestFailed(except.toString());
            }
        }   
    
    }
    

    My problem is that I'm getting this exception:

    IllegalStateExeption: UI engine accesed without hoding the lock of the event

    When you call llenarLista (...)  of requestSucceeded (...) who is called to LlamadaWSThread

    Please reread what I proposed. You missed a part on the displacement of campo in the body of the loop for?

    What you see, it's quite normal - plan you a change in the user interface that will update the display with the contents of an object, once you're done. Then you quickly change the contents of this object before all your regular screen updates has a chance to be executed. No wonder that all of these updates are only the last value of this object.

    Compare your loop for:

    for (int i = 0; i < campos.length; ++i) {
      final String campo = campos[i];
      UiApplication.getUiApplication().invokeLater(new Runnable() {
        public void run() {
          listaMensajes.add(new LabelField(campo));
        }
      });
    }
    

    You see the difference?

  • "Please wait" Popup goes away sooner than expected

    Hello

    My specs are:

    -Eclipse build IDE-20100218-1602

    -BlackBerry Java Plugin Version: 1.1.2.201004161203 - 16

    -BlackBerry Java SDK Version: 5.0.0.25

    I have a BrowserField2 which loads a specific page. While it loads I have 'Please wait' Popup but it goes far much more early to meet up with a blank screen until the page loads. This can confuse users.

    I want the Popup to stay until the page is loaded to 100%.

    What I am doing wrong?

    Heres part of my code:

    protected void onUiEngineAttached(boolean attached)    {        if(attached)        {            try            {             //Instantiate Web Content Request             GetWebContent getRequest = new GetWebContent(_browserField, _request);                //Pass background process and waiting message             PleaseWaitPopupScreen.showScreenAndWait(getRequest, _res.getString(LOADING_NEWS));            }            catch(Exception e)            {                                deleteAll();                add(new LabelField("ERROR:\n\n"));                add(new LabelField(e.getMessage()));            }        }    }
    
        public class GetWebContent implements Runnable     {       public GetWebContent(BrowserField _browserField, BrowserFieldRequest _request){           //constructor        }      public void run(){            //Get the Web Content for the BrowserField            _browserField.requestContent(_request);       }    }
    

    Here are some others:

    public static void showScreenAndWait(final Runnable runThis, String wait_message) {
            final PleaseWaitPopupScreen thisScreen = new PleaseWaitPopupScreen(wait_message);
            Thread threadToRun = new Thread() {
                public void run() {
                    // First, display this screen
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            UiApplication.getUiApplication().pushScreen(thisScreen);
                        }
                    });
                    // Now run the code that must be executed in the Background
                    try
                    {
                        runThis.run();
                    } catch (Throwable t)
                    {
                        t.printStackTrace();
                        throw new RuntimeException("Exception detected while waiting: " + t.toString());
                    }
                    // Now dismiss this screen
                    UiApplication.getUiApplication().invokeLater(new Runnable() {
                        public void run() {
                            UiApplication.getUiApplication().popScreen(thisScreen);
                        }
                    });
                }
            };
            threadToRun.start();
        }
    

    Help, please!

    you add the listener to the browserfield in your code.

    all you have to do now is to move the popup screen control in the listener methods.

    I would guess that abandoned, error etc all undisplay the popup, identical to documentLoaded.

    using the

    http://www.BlackBerry.com/developers/docs/6.0.0api/NET/rim/device/API/browser/Field2/BrowserFieldLis... net.rim.device.api.browser.field.ContentReadEvent)

    You can also view a progress if you wish.

  • Progress bar

    I want to add a progress bar when the connection is established. Please help me...

    There is an error in this code, sorry.  I; ve created a number of other samples, look for 'Please wait '.

    And in fact, I created another even simpler around Sample 1 I me round to implement.  But you can find it here:

    http://supportforums.BlackBerry.com/T5/Java-development/PleaseWaitPopupScreen/m-p/544983#M110325

Maybe you are looking for