Welcome to the Official BlackBerry® Support Community Forums. This is your resource to discuss support topics with your peers, and learn from each other. New to the forum? Please visit the ‘Getting Started’ link below.
inside custom component

Java Development

Reply
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX
Accepted Solution

Re: rotate status bar image on timer

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

 

 

Please use plain text.
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX

Re: rotate status bar image on timer

I get an IllegalStateException with an assertHaveEventLock...

Please use plain text.
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX

Re: rotate status bar image on timer

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

 

Please use plain text.
Developer
juanmab
Posts: 94
Registered: 07-08-2011

Re: rotate status bar image on timer

[ Edited ]

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

 


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.

Juanma 

@juanmab

Please use plain text.
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX

Re: rotate status bar image on timer

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

 

Please use plain text.
Developer
dnepr
Posts: 723
Registered: 03-12-2009

Re: rotate status bar image on timer

You are blocking the event thread among other things.

Use invokeLater inside the timer task. You also don't need to create the timer in a separate thread.
Please use plain text.
Developer
juanmab
Posts: 94
Registered: 07-08-2011

Re: rotate status bar image on timer

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


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.

Juanma 

@juanmab

Please use plain text.
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX

Re: rotate status bar image on timer

@depnr : can you give me an example?

Please use plain text.
Developer
dnepr
Posts: 723
Registered: 03-12-2009

Re: rotate status bar image on timer

Sure, it's very simple.


If drawing by hand:
1. Create a timer and timer task
2. Inside the timertask iterate over angles (i.e 0, 90, 180, 270) and call invalidate to force a repaint.
3. Draw / rotate the image via drawBitmap or drawTexturedPath depending on if angle is 0 or rotation is needed.

If just updating the bitmapfield:
1. same as above
2. precache n images for the desired angles
3. use invokeLater inside the timertask to set the new bitmap based on angle
4. Might need to call invalidate after setting the bitmap on some OSes in case there is a "feature" which doesn't repaint it properly after setting.
Please use plain text.
Regular Contributor
heather_1
Posts: 83
Registered: 05-24-2011
My Carrier: XXX

Re: rotate status bar image on timer

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

Please use plain text.