how to stop Browserfield requestContent() method when back to mainScreen on blackberry - blackberry

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.

Related

cannot close the screen in blackberry

I am using finish() to close current activity before quit application in Android.
However, I cannot close screen in blackberry.
public class Main_AllLatestNews extends MainScreen {
public Main_AllLatestNews() {
super(USE_ALL_WIDTH);
}
private boolean Dialog() {
final Bitmap logo = Bitmap.getBitmapResource("icon.png");
d = new Dialog("确定离开?", new String[] { "是", "否" }, new int[] {
Dialog.OK, Dialog.CANCEL }, Dialog.OK,
logo) {
public void setChangeListener(FieldChangeListener listener) {
if (d.getSelectedValue() == Dialog.OK) {
} else {
d.close();
}
};
};
d.show();
return (d.doModal() == Dialog.OK);
}
public boolean onClose(){
if(Dialog()){
System.exit(0);
return true;
}else
return false;
}
}
Here is my Main class
public class Main extends UiApplication {
public static void main(String[] args) {
Main theApp = new Main();
theApp.enterEventDispatcher();
}
public Main() {
pushScreen(new MyScreen());
}
public final class MyScreen extends MainScreen {
private Bitmap logo = Bitmap.getBitmapResource("logo_page.png");
private BitmapField bmfield;
public MyScreen() {
setTitle("Oriental Daily");
bmfield = new BitmapField(logo, Field.FIELD_HCENTER
| BitmapField.FOCUSABLE) {
protected boolean navigationClick(int status, int time) {
Main.this.pushScreen(new Main_AllLatestNews());
Main.this.popScreen(MyScreen.this);
return true;
}
};
}
}
It depends on exactly how you want your close behaviour to work. Also, I can only read English, so I'm not 100% sure what your Dialog says. I'm assuming it's something to do with closing the app (yes or no)?
Anyway, usually, my apps close by overriding the onClose() method in the MainScreen subclass. You don't actually need to listen for the escape key. onClose() will get called normally when the user escapes all the way out of the app, or presses the little button with the blackberry icon, and then selects Close.
public final class MyScreen extends MainScreen {
/** #return true if the user chooses to close the app */
private boolean showDialog() {
Bitmap logo = Bitmap.getBitmapResource("icon.png");
Dialog d = new Dialog("确定离开?",
new String[] { "是", "否" },
new int[] { Dialog.OK, Dialog.CANCEL },
Dialog.OK,
logo);
return (d.doModal() == Dialog.OK);
}
/** Shutdown the app? */
public boolean onClose() {
if (showDialog()) {
System.exit(0);
return true;
} else {
// the user does not want to exit yet
return false;
}
}
}

Blackberry - How to make a clock count down and display on screen

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;

blackberry buttonfield navigate on 2 second hold

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();

How to set Font of String Taken from Resource bundle in blackberry

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));

BlackBerry BrowserField IllegalStateException

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");
}
});
}
}

Resources