Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Background blackberry task during spashscreen

package mypackage;

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;

import java.util.*;

public class SplashScreen extends MainScreen {
   private MainScreen next;
   private UiApplication application;
   private Timer timer = new Timer();
   
   static EncodedImage eih1 = EncodedImage.getEncodedImageResource("splash.png");
   //EncodedImage my1 = EncodedImage.createEncodedImage(_bitmap1, 0, _bitmap1.getWidth());
   private static final EncodedImage _bitmap2 = MAMClient.sizeImage(eih1,Display.getWidth(), Display.getHeight());
   //private static final Bitmap _bitmap = Bitmap.getBitmapResource("SplashScreen.png");
   private static final Bitmap _bitmap = MAMClient.cropBitmap(_bitmap2.getBitmap(),Display.getWidth(),Display.getHeight());
   
   
   public SplashScreen(UiApplication ui, MainScreen next) {
      super(Field.USE_ALL_HEIGHT | Field.FIELD_RIGHT);
      this.application = ui;
      this.next = next;
      this.add(new BitmapField(_bitmap));
      SplashScreenListener listener = new SplashScreenListener(this);
      this.addKeyListener(listener);
      timer.schedule(new CountDown(), 5000);
      application.pushScreen(this);
   }
   public void dismiss() {
      timer.cancel();
      application.popScreen(this);
      application.pushScreen(next);
   }
   private class CountDown extends TimerTask {
      public void run() {
         DismissThread dThread = new DismissThread();
         application.invokeLater(dThread);
      }
   }
   private class DismissThread implements Runnable {
      public void run() {
         dismiss();
      }
   }
   protected boolean navigationClick(int status, int time) {
      dismiss();
      return true;
   }
   protected boolean navigationUnclick(int status, int time) {
      return false;
   }
   protected boolean navigationMovement(int dx, int dy, int status, int time) {
      return false;
   }
   public static class SplashScreenListener implements
      KeyListener {
      private SplashScreen screen;
      public boolean keyChar(char key, int status, int time) {
         //intercept the ESC and MENU key - exit the splash screen
         boolean retval = false;
         switch (key) {
            case Characters.CONTROL_MENU:
            case Characters.ESCAPE:
            screen.dismiss();
            retval = true;
            break;
         }
         return retval;
      }
      public boolean keyDown(int keycode, int time) {
         return false;
      }
      public boolean keyRepeat(int keycode, int time) {
         return false;
      }
      public boolean keyStatus(int keycode, int time) {
         return false;
      }
      public boolean keyUp(int keycode, int time) {
         return false;
      }
      public SplashScreenListener(SplashScreen splash) {
         screen = splash;
      }
   }
}

 

 

 

How can I during the 5 second spalshscreen, do some background work?? Bring some data from local database for example. 1000 data.

Developer
Posts: 134
Registered: ‎08-03-2011
My Device: torch, Z10
My Carrier: Vodafone, Airtel

Re: Background blackberry task during spashscreen

[ Edited ]

Can u check this code. 

 

public class SplashScreen extends MainScreen 
{ 	 
	public SplashScreen() 
	{ 		
		super(USE_ALL_WIDTH | USE_ALL_HEIGHT | NO_VERTICAL_SCROLL);

		VerticalFieldManager vfmMain = new VerticalFieldManager()
		{
			protected void sublayout(int maxWidth, int maxHeight) 
			{
				super.sublayout(Display.getWidth(), Display.getHeight());
				setExtent(Display.getWidth(), Display.getHeight());
			};
		};
		vfmMain.setBackground(BackgroundFactory.createBitmapBackground(Bitmap.getBitmapResource("splashimg.png")));
		add(vfmMain);

		UiApplication.getUiApplication().invokeLater(new Runnable() {

			public void run() {

				/* here call the code for Bring data from local database*/
				if(UiApplication.getUiApplication().getActiveScreen() instanceof SplashScreen)
					UiApplication.getUiApplication().popScreen(UiApplication.getUiApplication().getActiveScreen());

				/* here push new screen which you want after splash screen*/
			}
		});
	}
}

 

-----------------------------------------------------------------------
"Like" if you liked the post.
"Accept as Solution" if the post solves your question.
-----------------------------------------------------------------------
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Background blackberry task during Splash Screen

[ Edited ]

The code suggested by BrajeshSanodiya may actually cause your application to fail, because the intialization code is being run in the Event Tread. 

 

A lot of people think that

UiApplication.getUiApplication().invokeLater(<Runnable>);

is executing the <Runnable> in a different Thread, and to a certain extent it is, however it more accurate to say it is running as a separate invocation on the Event Thread.  Because the <Runnable> is running on the Event Thread it has exactly the same rules as any code running on the Event Thread and the result is any blocking operations or long running processes in the initialzation can cause the OS to think that the Event Thread is blocked and it will terminate the process.  The result is that your start-up processing will fail.

 

To run this properly is no more complicated than the invokeLater solution, simply replace the invokeLater with a start() for a new Thread.  Here is some sample code: 

 

public class SplashScreen extends MainScreen 
{ 	 
 public SplashScreen() 
 { 		
...
   Thread initThread = new Thread() {
    public void run() {
     // add initialisation code here
     // such loading data from database or scaling images
     UiApplication.getUiApplication().invokeLater(new Runnable() {
      public void run() {
       // Add any UI dependent code in here, 
       // such as popping the Splash Screen or 
       // pushing your first real screen
      }
     });
    }
   };
   initThread.start();
  }
}

 

One thing I really like about coding the SplashScreen in this way, is that you do not have to define your next screen before you start your Splash Screen.  So for example, if you have some problems during the initialization, you can report this to the user in your 'next' screen.  Or, and just as usefully, you can use things like data from a database or images that need to be scaled in the "next" screen.  And you can even do things like network operatinos from this background initThread Thread while your SplashScreen is on display. 

 

I hope my sample code is clear, if not, please come back to me.

Highlighted
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Background blackberry task during Splash Screen

Now that you have reviewed the above, I'm actually going to thrown in a complication.

 

It is possible that you do not want the initialization code to start until until your UiApplication is created and being dispatched.  As an example (not a very good one possibly), you have some Permissions checking that you do, and you want to make 100% sure that your SplashScreen is on display before the user sees the request for permissions.

 

The initThread suggested in the last post potentially gets started before your Application has completed starting up. 

 

So to make 100% sure that the user sees your Splash Screen first, the easiest thing to do is to start the initThread in the onUiEngineAttached() method of the SplashScreen, as follows:

 

 protected void onUiEngineAttached(boolean attached) {
  super.onUiEngineAttached(attached);
  if ( attached ) {
   // initThread code in here
  }
 }

 Again, if you need anything more, please come back....