Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Highlighted
Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified
Accepted Solution

Display a splash screen when starting a background application.

Hi,

 

I have an application that runs in the background and is set to auto-start feature based on this:

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800451/800660/How_To_...

 

My question is... is it possible to display a splash screen when the application is launch the first time on Blackberry startup?

Sorry if this is an easy question, but I haven't made any UI yet on the BB using Java.

 

I just would like to have a few tips or sample code to start.

 

Thanks!

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

These two KB articles should give you everything you need to know.  You might not need to use the second one:

 

How To - Display a GUI when the BlackBerry device starts up
Article Number: DB-00539

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800620/How_To_...

 

How To - Alert a user from a Background application
Article Number: DB-00407

http://www.blackberry.com/knowledgecenterpublic/livelink.exe/fetch/2000/348583/800332/800505/800608/...

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

Thanks Peter!

 

It worked... without having to use the second link.

 

But... I would like to have the user click on a "OK" button before the application can begin... Is that possible?

In other words, I would like to display a GUI with a "OK" button (and some text of course) when my background thread is first started... then wait for the user to press the "OK" button and then continue the execution of the remaining code.

 

Now, with your help, I'm able to display a GUI with an "OK" button when my background thread is starting... but the remaining code is executed right away without having to wai for the user to hit the button.

 

Is that something I could do?

 

Thanks again!

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

Two thoughts come immediately to mind:

 

1) Use a modal method to display your DIalog (InvokeAndWait or Dialog.doModal();

 

2) Use a DialogClosedListener to detect the dismissing of the Dialog and put the continuation of your Application startup in there. 

 

If I was going this, I think I'd go for (2) - it is more complicated but my instinct tells me that it is less likely to get in the way of other processing.  But I've never done anything like what you are trying to do, so no guarantees that either will work.

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

Thanks Peter,

 

I wanted to try your 2nd suggestion... but I'm facing a problem.

I have this piece of code (in the run() method):

            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    Dialog.inform("You are being tracked...");
                }
            });
            UiApplication.getUiApplication().requestForeground();

 

My dialog is displayed... but it doesn't close when I click on the "OK" button.

 

I'm pretty sure this is related to the fact I'm using a background thread.

There's probably a simple reason for this... but I'm still a bit confused about using UI in a background thread.

 

Should I put the above code somewhere before I create a new background thread?  Or in the run() method?

Should I start my thread in the DialogClosedListener class after the user clicked "OK"?

 

Sorry if this is obvious, but I'm having some problems putting all this together.

 

Thanks again!

 

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

Sorry this is probably getting a bit beyond what I can visualize without actually seeing it, but I'll try.

 

First some random thoughts.  Do you really want to extend UiApplication?  If you do, then your application will appear in the task switch list (you see this holding "ALT and ESC").  If you use the Task Switch and select your application, the Blackberry application will presume that there is a screen to display.  I'm beating you don't have a screen, so as a result, when you do this selection, I think you will see a 'white' screen.  I suspect once you are on this white screen, you will be stuck like you are with the Dialog. 

 

In fact I have a question, when the Dialog displays, does it display over the top of the Home Screen, or is it over the top of a white screen?

 

I presume also, that once you are in this 'stuck state' you can get at normal BlackBerry processing by using the ALT-ESC and selecting the Home screen - is this correct?

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

Here are my answers...

 

"Do you really want to extend UiApplication?"

- At first, I was extending Application but Mark kindly told me (on another post) I should extend UiApplication cause I'm using network connections... and that without the UiApplication, the user could not agree to the firewall prompts.

 

"If you do, then your application will appear in the task switch list"

- Yes it does... and I would rather not have it there.

 

"If you use the Task Switch and select your application, the Blackberry application will presume that there is a screen to display.  I'm beating you don't have a screen, so as a result, when you do this selection, I think you will see a 'white' screen."

- Actually, I don't see a white screen.  I just see the task switch list as if my Blackberry was frozen.  I can't do anything unless I press the red phone button and then I'm back to the homepage (even the ESC key won't do anything).

 

"In fact I have a question, when the Dialog displays, does it display over the top of the Home Screen, or is it over the top of a white screen?"

- Over the top of a white screen.

 

"I presume also, that once you are in this 'stuck state' you can get at normal BlackBerry processing by using the ALT-ESC and selecting the Home screen - is this correct?"

- No, it looks like I can't do anything unless I press the red phone button.

 

I would have liked to have a background thread that run on Blackberry startup, show a dialog, then when the user click on "OK", then begin my code in the background (and if possible... not display the application in the task switch list).

 

This is my first BB application so I'm a bit confused about all those background thread/UI thing.

 

Thanks for your help... I really appreciate.

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

H'm interesting one.....

 

Mark is right, you are going to have to extend UiApplication.  So you are stuck with having your icon in the Task switch list, and this means that someone can select it.  So you are going to need to have a screen that displays something innocuous.  I'm guessing you could to override the onExposed() method to immediately push the application to the background, but that is not very friendly.  I would suggest something like an About screen.  This screen need not do anything except allow the user to push it to the background (i.e. UiApplication.getUiApplication.requestBackground()).

 

Before we start looking at your other issues, I think we need to resolve this.  I think you need to create a Screen associated with your application.  I do this, I just have the screen requestBackground as part of the constructor so you only see it if you click on the Task Switch icon.

 

In fact, you could actually use this in your start up.  Instead of pushing the Dialog, you could push this screen, then instead of 'OK', the User could just ESC from this screen, or you could have a button on it, either just does the request to the background.

 

"This is my first BB application so I'm a bit confused about all those background thread/UI thing".  This is not the sort of app that I would recommend for a first timer.  You've done well getting this far!

Developer
Posts: 117
Registered: ‎07-16-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

I think that all you said makes a lot of sense.  I really like your idea of an about screen... as this will achieve my requirement of having a splash screen when the application start... and will also let the user know that the application is still running (when he clicks on the icon manually or when he goes into the switch list).

 

I tried it... here's my code so far:

        public void run() {           
            ApplicationManager myApp = ApplicationManager.getApplicationManager();
            while(myApp.inStartup()) {
                try {
                    _thread.sleep(2000);
                } catch(Exception e) {
                    // Catch Exception
                }
            }

            UiApplication.getUiApplication().invokeLater(new Runnable() {
                public void run() {
                    pushScreen(new AboutScreen());
                }
            });
            UiApplication.getUiApplication().requestForeground();

 

            // more code here....

 

       }

 

     private final class AboutScreen extends MainScreen {
        public AboutScreen() {
            super(); // superclass constructor [= new MainScreen()]
            setTitle("GPS");
            add(new SeparatorField());
            LabelField txt = new LabelField("You're being tracked!");
            ButtonField btn = new ButtonField("CLOSE");
            btn.setChangeListener(new ButtonListenerImpl());
            add(txt);
            add(btn);
        }
    }
    
    private final class ButtonListenerImpl implements FieldChangeListener {
        public void fieldChanged(Field field, int context) {
            UiApplication.getUiApplication().requestBackground();
       }
    }

Thanks to you, this is already a better solution than my previous one.  However, I have some issue with it.

#1- This is more like a look issue than a real one... but would it be possible to close the window by just clicking on the enter key (or the track wheel) without having to display a button (like most about screen, just a click anywhere on the screen close it).  This is not a real issue.

 

#2- This is more important... when I click on the ESC key, I'm often prompted by the "Changes made! Save/Discard/Cancel" screen.  As you can see in my above code, my screen consist of only a label and a button so I don't know what has changed.  It seems to happen when the "Close" or "Full Menu" are open.

 

#3- This is the most important... when I click on the ESC key, whether I'm prompted (and I choose Save or Discard) or not, my application close completely (not just the screen but the whole application).

 

But, as long as I click on my button, my application is still running fine... and the screen is set to the background... which is really cool!

 

Thanks again!  You are really helpful!

 

BTW: "This is not the sort of app that I would recommend for a first timer.  You've done well getting this far!"

Unfortunaly, I didn't have to choose my first application Smiley Happy

Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Display a splash screen when starting a background application.

1. Yes.  Your AboutScreen is extending MainScreen, so key strokes will go through MainScreen's keyChar and scroll clicks will go through navigationClick.  So you just need to override these, which means you need to add  something like the following within your AboutScreen:

 

// Regardless of character pushed, close screen...
    public boolean keyChar(char key, int status, int time) {
        close();

        return true;

    }

 // Close on click too

    protected boolean navigationClick(int status,  int time) {

        close();

        return true;

    }

    private void close() {

        UiApplication.getUiApplication().requestBackground();
    }

 

2.  This is because you go through the standard onClose method, which checks if the screen is dirty (i.e. changed) and will put this message up.  There are many posts about this problem, have a look round.  However because you are trapping the ESC with the keyChar method above, you won't go through the standard processing anymore.

 

3.  Again, this is because you go through the standard processing which exits you app.  Again with the changes made in 1, you won't go through the standard processing and won't exit.