08-03-2011 10:43 AM
Hi,
I am trying to rotate the bitmapfield in my status bar on 3 second intervals. I know threading is involved in some manner but can't figure it out. Here is what I have so far. Any help would be appreciated!
new Thread(){
public void run(){
try {
timer = new Timer();
timer.scheduleAtFixedRate(new ImageTask(), 0, 5000);
} catch (Exception e) {
// do nothing
}
}
}.start(); private class ImageTask extends TimerTask {
public void run() {
System.out.println("Test Printing..");
setStatus(adField2);
}
}
Solved! Go to Solution.
08-03-2011 10:46 AM
I get an IllegalStateException with an assertHaveEventLock...
08-03-2011 10:58 AM
I am trying this now...still not changing image, but no error
new Thread(){
public void run(){
System.out.println("run entered");
try {
System.out.println("in try");
timer = new Timer();
sleep(3000);
setStatus(adField1);
sleep(3000);
setStatus(adField2);
sleep(3000);
setStatus(adField);
//timer.scheduleAtFixedRate(new ImageTask(), 0, 3000);
} catch (Exception e) {
e.getMessage();
}
}
}.start();
08-03-2011 11:02 AM - last edited on 08-03-2011 11:06 AM
I guess the problem is that you can't modify the UI being outside de main thread or without having the event lock.
Look for an example of threads using invokeLater().
That will do it.
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
//some code lalalala
}
});
Juanma
08-03-2011 11:27 AM
ok not error now, but can anyone tell me why this code is now rotating the images?
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
System.out.println("in try");
timer = new Timer();
try {
synchronized (this){
wait(3000);
setStatus(adField1);
notify();
}
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
synchronized (this){
wait(3000);
setStatus(adField2);
notify();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
synchronized (this){
wait(3000);
setStatus(adField);
notify();
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//timer.scheduleAtFixedRate(new ImageTask(), 0, 3000);
}
});
08-03-2011 11:35 AM
08-03-2011 11:39 AM
As I'm new so I don't now why you are using synchronized o the notify.
Did you try without that?
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
try {
sleep(3000);
setStatus(adField1);
sleep(3000);
setStatus(adField2);
sleep(3000);
setStatus(adField);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
});
Remember to give kudos when appropriate
Juanma
08-03-2011 01:09 PM
@depnr : can you give me an example?
08-03-2011 02:37 PM
08-03-2011 03:26 PM
I got it figured out...I am using this code:
final Timer t=new Timer();
TimerTask tt=new TimerTask() {
public void run() {
if(ad_time % 3 == 0)
adField.setBitmap(CreativeCore.ad2);
if(ad_time % 3 == 1)
adField.setBitmap(CreativeCore.ad3);
if(ad_time % 3 == 2)
adField.setBitmap(CreativeCore.ad);
ad_time++;
invalidate();
}
};
t.scheduleAtFixedRate(tt, 1000, 3000);