04-11-2012 03:36 AM - edited 04-11-2012 03:37 AM
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.
Solved! Go to Solution.
04-11-2012 04:15 AM
04-11-2012 05:37 AM
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 below the post(s) that helped you.
2. Please resolve your thread by marking the post "Solution?" which solved it for you!
04-11-2012 07:38 AM
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?
04-11-2012 07:45 AM
04-11-2012 08:03 AM
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 ![]()
04-11-2012 08:16 AM
04-16-2012 06:10 AM
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.FIEL
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(thisSc
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.FIEL
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(thisSc
System.exit(0);
}
});
}
}
protected boolean onSavePrompt() {
return true;
}
…......