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
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Carrier: none
Accepted Solution

How to make countdown seconds and skip to another screen?

[ Edited ]

Hello, this is my first message. I need your help:

I want to do a screen with a coundown in seconds. When seconds equal 0, skip to another screen.

How could I make it?

 

Thank you very much.

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: How to make countdown seconds and skip to another screen?

welcome to the support forums.

i would suggest to use a timertask, initialize it with your second value and count them down, when they are 0 you push a new screen and cancel the timertask.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
hrushikeshsalkade
Posts: 17
Registered: ‎12-20-2011
My Carrier: Vodafone

Re: How to make countdown seconds and skip to another screen?

Hi 

You can use this code

 

 

final Timer timer = new Timer();
		        final TimerTask task = new TimerTask() {
				// delay.	
		        	int sec = 5;
					public void run() {
						sec --;
						if(sec == 0){
							UiApplication.getUiApplication().invokeLater(new Runnable() {
								
								public void run() {
									//TODO change your screen here...
								}
							});
						}
					}
				};
				
				timer.scheduleAtFixedRate(task, 0, 1000);

 1. If any post helps you please click the Like Button below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!

Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Carrier: none

Re: How to make countdown seconds and skip to another screen?

Thank you.

I have used it, it´s a good idea. But now I have a problem. How I could stop all proccess, show the screen with countdown and , when finish countdown, continue the execution? With Thread sleep?

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: How to make countdown seconds and skip to another screen?

that depends on what "all processes" are.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Carrier: none

Re: How to make countdown seconds and skip to another screen?

Ok, look. I will try to explain better.

I have a screen, when I push a button, I have in the FieldChangeListener:

 

ScreenManagement.myPushScreen(new CountdownScreen("Countdown"));

ScreenManagement.myPushScreen(new PrintingScreen("Printing"));

 

I want that it show CountdownScreen, pass the seconds, and then, show PrintingScreen.

 

Thank you again :smileyhappy:

Please use plain text.
Developer
simon_hain
Posts: 15,558
Registered: ‎07-29-2008
My Carrier: O2 Germany

Re: How to make countdown seconds and skip to another screen?

you could push them the other way around (printing first, countdown later) and just close the countdown screen after the countdown.
or push the printing screen from the countdown screen, as you like.

maybe peters samples are helpful:
http://supportforums.blackberry.com/t5/Java-Development/Sample-quot-Please-Wait-quot-screen-part-1/t...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
Contributor
frandelbetis
Posts: 42
Registered: ‎03-17-2012
My Carrier: none

Re: How to make countdown seconds and skip to another screen?

Thanks to all. This is the solution to show an ad while a countdown is running. When finish the countdown, show another screen:

 

In Main.java:
…..
// Show ad screen
final AdScreen adScreen = new AdScreen();
pushScreen(adScreen);
…...

In AdScreen.java use TimerTask for the countdown, and Banner class for add the ad. Constantes.IDPUBLICIDAD is the id when you register in Advertising Service, and imagenDeEsperaPublicidad is a Bitmap with the waiting image while ad is loading:
….
       Banner bannerAd = new Banner(Constantes.IDPUBLICIDAD, null, 100000, Constantes.imagenDeEsperaPublicidad);
       bannerAd.setBorderColor(Color.WHITE);
       bannerAd.setBannerTransitionColor(Color.WHITE);
       bannerAd.setMMASize(Banner.MMA_SIZE_EXTRA_LARGE);
       bannerAd.setTestModeFlag(false);
       HorizontalFieldManager hfm = new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
       hfm.add(bannerAd);
       add(hfm);

       // Countdown
       countdown = new LabelField("Wait" + Constantes.SEGUNDOS + "  seconds for next screen...");
       add(countdown);

       final Screen thisScreen = this;
       segundos=Constantes.SEGUNDOS;
       timerTask = new TimerTask() {
           public void run() {
               if (segundos > 1) {
                   segundos--;
                   UiApplication.getUiApplication().invokeLater(new Runnable() {
                       public void run() {
                               countdown.setText(""Wait" + Constantes.SEGUNDOS + "  seconds for next screen...");
                       }
                   });
               } else {
                   cancel();
                   UiApplication.getUiApplication().invokeLater(new Runnable() {
                   public void run() {
                       // When coundown finish, pop main screen and push next screen               
                       UiApplication.getUiApplication().popScreen(thisScreen);
                       UiApplication.getUiApplication().pushScreen(new NextScreen());    
                   }
               });

               }
           }
       };
      //Start timer
       Timer timer = new Timer();
       // In 0 ms do something each1000 ms (1 second)
       timer.scheduleAtFixedRate(timerTask, 0, 1000);

//Cancel button
       HorizontalFieldManager hz=new HorizontalFieldManager(HorizontalFieldManager.FIELD_HCENTER);
       cancel = new ButtonField("Cancel", ButtonField.CONSUME_CLICK);
       hz.add(cancel);
       add(hz);
       cancelar.setChangeListener(this);
   }

   public void fieldChanged(Field field, int context) {
       if (field == cancel) {
           final Screen thisScreen = this;
           UiApplication.getUiApplication().invokeLater(new Runnable() {
               public void run() {
                   segundos=0;
                   UiApplication.getUiApplication().popScreen(thisScreen);
                   System.exit(0);
                   
               }
           });
       }
   }

   protected boolean onSavePrompt() {
       return true;
   }
…......


Please use plain text.