I am making a quiz application with question and answer.
I want to make an clock count down ( Minute + Second ) and display it on screen.
But I can not how to do that.
Can anyone help me please ?
this is one solution to print time mm:ss format (Minutes:seconds)
public class StartUp extends UiApplication{
public static void main(String[] args) {
StartUp start=new StartUp();
start.enterEventDispatcher();
}
public StartUp() {
pushScreen(new Timerscreen());
}
}
class Timerscreen extends MainScreen
{
LabelField time;
long mille=0;
Timer timer=null;TimerTask task=null;
public Timerscreen() {
mille=1000*60*1;
time=new LabelField();
add(time);
timer=new Timer();
task=new TimerTask() {
public void run() {
synchronized (UiApplication.getEventLock()) {
if(mille!=0){
SimpleDateFormat date=new SimpleDateFormat("mm:ss") ;
System.out.println("================="+date.formatLocal(mille)+"====================="+Thread.activeCount());
time.setText(date.formatLocal(mille));
mille=mille-1000;
}else{
time.setText("00:00");
mille=1000*60*1;
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.inform("Time expaired");
}
});
}
}
}
};
timer.schedule(task,0, 1000);
}
}
Same like above but some change:
First set:
int secs=120; // how many seconds you want;
public void callTheTimer()
{
label=new LabelField("\t");
add(label);
timer=new Timer();
timerTask=new TimerTask()
{
public void run()
{
synchronized (UiApplication.getEventLock())
{
if(secs!=0)
{
label.setText(""+(secs--)+" secs");
}
else
{
timer.cancel();
UiApplication.getUiApplication().invokeLater(new Runnable()
{
public void run()
{
label.setText("");
Dialog.alert("Times Up");
secs=120;
}
});
}
}
}
};
timer.schedule(timerTask, 0, 1000);
}
This is enough;
Related
Timer timer = new Timer();
timer.schedule(new TimerTask()
{
public void run()
{
InputMethodManager inputManager =(InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}, 998);
this is java
**
I want to delay the keyboard pop-up**
public class MyActivity: IRunnable{
Android.OS.Handler _handler;
protected void MethodXY(){
_handler = new Android.OS.Handler();
_handler.PostDelayed(this, 998);
}
public void Run()
{
_handler.RemoveCallbacks(this);
InputMethodManager inputManager =(InputMethodManager)editText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(editText, 0);
}
}
Alternative: (e.g. you are using more than one Handler in your activity)
public class MyActivity: IRunnable{
protected void MethodXY(){
Android.OS.Handler handler = new Android.OS.Handler();
handler.PostDelayed(new DelayedKeyboardRunnable(handler, editText), 998);
}
private class DelayedKeyboardRunnable: Java.Lang.Object, IRunnable
{
EditText _et;
Android.OS.Handler _handler;
public DelayedKeyboardRunnable(Android.OS.Handler handler, EditText et)
{
_handler = handler;
_et = et;
}
public void Run()
{
_handler.RemoveCallbacks(this);
InputMethodManager inputManager = (InputMethodManager)_et.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(_et, 0);
}
}
}
I am developing a blackberry app. I want to open an screen from the home screen when user will press one button and hold it for 2 seconds.
Any way?
Thanks.
Here is the code for your question. I have make use of this BlackBerry LongClickListener implementation link which contains good explanation too.
public class HoldButtonScreen extends MainScreen implements FieldChangeListener {
ButtonField _btnHold;
Timer _timer;
public HoldButtonScreen() {
_btnHold = new ButtonField("Hold 2 sec to get popup")
{
protected boolean navigationClick(int status, int time) {
final Field _btnHold= this;
_timer = new Timer();
System.out.println("hi there");
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try{
_timer.schedule(new TimerTask() {
public void run() {
fieldChanged(_btnHold, 0);
}}, 2000);
}catch(Exception e){
e.printStackTrace();
}
}
});
return true;
}
protected boolean navigationUnclick(int status, int time) {
System.out.println("hi unclick");
add(new LabelField("You have't hold button for 2 second."));
_timer.cancel();
return true;
}
};
_btnHold.setChangeListener(this);
add(_btnHold);
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
PopupScreen _popUpScreen = new PopupScreen(new VerticalFieldManager()){
public boolean onClose() {
close();
return true;
}
};
_popUpScreen.add(new LabelField("Hello , i am pop up after 2 second."));
UiApplication.getUiApplication().pushScreen(_popUpScreen);
}
});
}
}
Try this this will run successfully.
Just change the Screen in PushScreen.
private Thread splashTread;
protected int _splashTime = 200;
boolean countinue = true;
splashTread = new Thread() {
public void run() {
while (countinue == true) {
try {
synchronized (this) {
wait(_splashTime);
}
} catch (InterruptedException e) {
} finally {
synchronized (UiApplication.getUiApplication().getAppEventLock()) {
UiApplication.getUiApplication().pushScreen(new Login());
SplashScreen.this.close();
}
countinue = false;
}
}
}
};
splashTread.start();
When i click a button, in next screen i've loaded browserfield requestcontent() using thread.
and i've added browserfield listener.
when click back button , i came to first screen. But in background, requestContent is executing. how to stop it.?
i write the code on onClose() method
public boolean onClose()
{
for(int i=0;i<=Thread.activeCount();i++)
{
if(Thread.currentThread().isAlive())
{
Thread.currentThread().interrupt();
}
}
return super.onClose();
}
My code is.,
new Thread(new Runnable()
{
public void run()
{
loadWebContent(path);
}
}).start();
private void loadWebContent(String path)
{
final VerticalFieldManager vfm = new VerticalFieldManager(HORIZONTAL_SCROLL|VERTICAL_SCROLL)
{
protected void sublayout(int maxWidth, int maxHeight)
{
super.sublayout(maxWidth, (Display.getHeight()-47));
setExtent(maxWidth, (Display.getHeight()-47));
}
};
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
myBrowserField = new BrowserField(myBrowserFieldConfig);
myBrowserField.addListener(new BrowserFieldListener()
{
public void documentLoaded(BrowserField browserField,Document document) throws Exception
{
UiApplication.getApplication().invokeLater(new Runnable()
{
public void run()
{
try
{
mainlayout.delete(spinner);
mainlayout.add(myBrowserField);
myBrowserField.setZoomScale(1.0f);
}
catch (Exception e)
{
System.out.println("++ "+e);
}
}
});
}
});
myBrowserField.requestContent(path);
}
Pls help how to stop execution when back to first screen.
I think this is Enough:
public class LoadingScreen extends MainScreen
{
ButtonField click;
String url;
BrowserField browserField;
public LoadingScreen()
{
url="http://www.google.com";
browserField=new BrowserField();
add(browserField);
browserField.requestContent(url);
}
}
In onClose method:
public boolean onClose()
{
return super.close();
}
If you have any doubbts come on chat room named "Knowledge sharing center for blackberry and java" to clarify your and our doubts.
I'm giving localization support in my app in blackberry.Now the problem is i want to use system font in my application but the hieght should be constant.Now i want to use Status.show method to show some string on the screen.The string is coming from the resource bundle.
So kindly suggest how to set the font of that.
Thanx in advance
Regards
Deepak Goel
public class DialogBox extends PopupScreen implements FieldChangeListener {
private ButtonField okButton = new ButtonField("OK", ButtonField.CONSUME_CLICK);
private static DialogBox dialog = null;
private DialogBox( String text) {
super(new VerticalFieldManager(VerticalFieldManager.VERTICAL_SCROLL | VerticalFieldManager.VERTICAL_SCROLLBAR));
add(new LabelField(text, Field.FIELD_HCENTER));
okButton.setChangeListener(this);
add(okButton);
}
protected void paint(Graphics graphics) {
graphics.setColor(Color.WHITE);
graphics.fillRect(0, 0, getWidth(), getHeight());
graphics.setColor(Color.BLACK);
super.paint(graphics);
}
public static void show(String text) {
dialog = new BMDialogBox(text);
Thread threadToRun = new Thread() {
public void run() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
UiApplication.getUiApplication().pushScreen(dialog);
}
});
try {
sleep(2000);
} catch (Throwable t) {
t.printStackTrace();
throw new RuntimeException("Exception detected while waiting: " + t.toString());
}
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
});
}
};
threadToRun.start();
}
public void fieldChanged(Field field, int context) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
UiApplication.getUiApplication().popScreen(dialog);
} catch (Exception e) { }
}
});
}
}
you can set a fields font with original style of it and given height(15) just like that:
setFont(getFont().derive(getFont().getStyle(),15));
I'm trying to implement simple user interaction with field2.BrowserField: when button is clicked BrowserField loads
another page, but I'm getting IllegalStateException.
here's my code:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test2();
}
});
add(btn2);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
private void test2() {
synchronized (Application.getEventLock())
{
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
}
}
Try this one:
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1");
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test1();
test1("http://www.blackberry.com/developers");
}
});
add(btn1);
ButtonField btn2 = new ButtonField("test2");
btn2.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
//test2();
test1("http://www.blackberry.com/developers");
}
});
add(btn2);
browser = new BrowserField();
add(browser);
//browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
test1("http://stackoverflow.com/");
}
private void test1(final String url) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
browser.requestContent(url + ";deviceside=true");
}
});
}
// private void test2() {
// synchronized (Application.getEventLock())
// {
// browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
// }
// }
}
Wow, adding browser.setFocus(); do the trick
public class BrowserScreen extends MainScreen {
private BrowserField browser;
public BrowserScreen() {
super();
setTitle("Browser State example");
ButtonField btn1 = new ButtonField("test1", ButtonField.CONSUME_CLICK);
btn1.setChangeListener(new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
test1();
}
});
add(btn1);
browser = new BrowserField();
add(browser);
browser.requestContent("http://stackoverflow.com/" + ";deviceside=true;");
}
private void test1() {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
browser.setFocus();
browser.requestContent("http://www.blackberry.com/developers" + ";deviceside=true");
}
});
}
}